2017-06-12 12 views
0

favoritekafefMessageを保存し、onItemLongClickメソッドの終了後にfavoritekafeを返します。onItemLongClick(Android)を使用した後に文字列メッセージを保存

これはどんな方法でも可能ですか?

または、 "onItemLongClick"メソッドからtrue/false以外の変数を返す方法がありますか?私のコードザッツ

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import android.content.SharedPreferences; 

public class MainActivity2 extends AppCompatActivity { 

ListView kafeteries; 
String favoritekafe; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.coffee); 

    if (favoritekafe==null) { 
     Toast.makeText(getApplicationContext(), "you don't have a favorite 
    caffe yet", Toast.LENGTH_SHORT).show(); 
    } 
    else{ 
     Toast.makeText(getApplicationContext(), favoritekafe, 
    Toast.LENGTH_SHORT).show(); 
    } 



    /* oi kafeteries se lista */ 
    kafeteries = (ListView) findViewById(R.id.kafeteries); 

    ArrayAdapter<String> mAdapter = new ArrayAdapter<String> 
(MainActivity2.this, 
      android.R.layout.simple_list_item_1, 
      getResources().getStringArray(R.array.kafeteries_syros)); 

    kafeteries.setAdapter(mAdapter); 

    // mnmta TOAST otan kanw click kapoio stixeio tis listas */ 
    kafeteries.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      String sMessage = ""; 
      switch(position) { 
       case 0: sMessage = "Coffee and drinks with hospitable locals\nArea:Ermoupoli\nPhone:2281083354"; break; 
       case 1: sMessage = "Coffee in the narrow streets of the city\nArea:Ermoupoli\nPhone:2281079225"; break; 
       case 2: sMessage = "The smallest and most adorable coffee in town\nArea:Ermoupoli\nPhone:2281300880"; break; 
       case 3: sMessage = "Coffee and snacks at the city's harbor\nArea:Ermoupoli\nPhone:2281076144"; break; 
       case 4: sMessage = "The city's most famous café\nArea:Ermoupoli\nPhone:2281085337"; break; 
      } 
      Toast.makeText(getApplicationContext(), sMessage, Toast.LENGTH_LONG).show(); 
     } 
    }); 


    // prospathia gia long clik add sta favorite kai save*/ 
    kafeteries.setLongClickable(true); 
    kafeteries.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long id) { 

      // prepei na vrw ena tropo na epistrefw kai to position gia na apothikevw tin agapimeni epilogi */ 


      String fMessage = ""; 
      switch(position) { 
       case 0: fMessage = "Boheme del mar is your favorite caffe"; break; 
       case 1: fMessage = "Jar is favorite caffe"; break; 
       case 2: fMessage = "Kouchico is your favorite caffe"; break; 
       case 3: fMessage = "Okio is your favorite caffe"; break; 
       case 4: fMessage = "Plaza is your favorite caffe"; break; 
      } 
      Toast.makeText(getApplicationContext(), fMessage, Toast.LENGTH_SHORT).show(); 

      final SharedPreferences prefs=getApplicationContext().getSharedPreferences("settings",MODE_PRIVATE); 
      prefs.edit().putString(favoritekafe,fMessage).commit(); 

      return true; 
     } 

    }); 


    } 

} 
+0

ようfavoritekafe

は、なぜあなたはそれを返すようにしたいんFAにロード行う前のonCreateで、この

PreferenceManager.getDefaultSharedPreferences(context).edit() .putString("fav_kafe", favoritekafe).commit(); 

しますか?あなたはそれを 'favoritekafe = fMessage;'と同じにすることができ、onItemLongClick()は常にtrue/falseを返します。 – Adithya

+0

私はこの活動が乾杯するたびに欲しいから。 以前Listviewからアイテムを長時間クリックしたかどうかによって異なります。 (私の英語は申し訳ありません、それは私の母国語ではありません) –

答えて

1

onCreate

で値を取得し、あなたのクラス内

void gotStringFromLongClick(String fMessage){ 
    favoritekafe=fMessage; 
    // do anything else you want here 
} 

を関数を作成し、外部switch

String sMessage = ""; 
    switch(position) { 
       case 0: sMessage = "Coffee and drinks with hospitable locals\nArea:Ermoupoli\nPhone:2281083354"; break; 
       case 1: sMessage = "Coffee in the narrow streets of the city\nArea:Ermoupoli\nPhone:2281079225"; break; 
       case 2: sMessage = "The smallest and most adorable coffee in town\nArea:Ermoupoli\nPhone:2281300880"; break; 
       case 3: sMessage = "Coffee and snacks at the city's harbor\nArea:Ermoupoli\nPhone:2281076144"; break; 
       case 4: sMessage = "The city's most famous café\nArea:Ermoupoli\nPhone:2281085337"; break; 
      } 
    gotStringFromLongClick(fMessage); 
    // save value to preference 
    SharedPreferences.Editor editor = getSharedPreferences("pref", MODE_PRIVATE).edit(); 
    editor.putString("data", fMessage); 
    editor.commit(); 

からgotStringFromLongClick(fMessage);のようにそれを呼び出します

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.coffee); 
    favoritekafe = getSharedPreferences("pref", MODE_PRIVATE).getString("data", null); 
    if (favoritekafe==null) { 
     Toast.makeText(getApplicationContext(), "you don't have a favorite 
    caffe yet", Toast.LENGTH_SHORT).show(); 
    } 
    else{ 
     Toast.makeText(getApplicationContext(), favoritekafe, 
    Toast.LENGTH_SHORT).show(); 
    } 
+0

それは動作していません。私がこのアクティビティを開始するたびに、「favoritekafe」はヌルです –

+0

明らかに、まずsharedpreferenceに値を保持できます –

+0

'sharedpreference'で更新されたコードを試してください –

0

SharedPreferenceファイルに保存できます。あなたのonLongClickListener内部 は、トーストは、この

favoritekafe=PreferenceManager.getDefaultSharedPreferences(context) 
.getString("fav_kafe", "nothing"); 
if (favoritekafe.equals("nothing") { 
    Toast.makeText(getApplicationContext(), "you don't have a favorite 
caffe yet", Toast.LENGTH_SHORT).show(); 
} 
else{ 
    Toast.makeText(getApplicationContext(), favoritekafe, 
Toast.LENGTH_SHORT).show(); 
} 
関連する問題