2017-06-12 30 views
0

我想將fMessage存儲到變量favoritekafe並在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

你爲什麼想回到之前做到這一點

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

那麼的onCreate?你可以將它等同於'favoritekafe = fMessage;'和onItemLongClick()將始終返回true/false。 – Adithya

+0

因爲我想每次這個活動開始有烤麪包。 取決於您以前是否已經長時間點擊列表視圖中的項目。 (對不起我的英語,這不是我的本地人) –

回答

1

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);onCreate

檢索值
@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

這樣

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(); 
}