在使用onItemLongClick(Android)后存储字符串消息



我想将fMessage存储到变量favoritekafe,并在onItemLongClick方法结束后返回favoritekafe

这有可能吗?

或者我可以用不同的方式从"onItemLongClick"方法返回除真/假之外的变量吗?

这是我的代码:

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 localsnArea:ErmoupolinPhone:2281083354"; break;
                case 1: sMessage = "Coffee in the narrow streets of the citynArea:ErmoupolinPhone:2281079225"; break;
                case 2: sMessage = "The smallest and most adorable coffee in townnArea:ErmoupolinPhone:2281300880"; break;
                case 3: sMessage = "Coffee and snacks at the city's harbornArea:ErmoupolinPhone:2281076144"; break;
                case 4: sMessage = "The city's most famous cafénArea:ErmoupolinPhone: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;
        }
    });

    }
}

在类中创建函数

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

并称它为外部gotStringFromLongClick(fMessage); switch

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

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

您可以将其存储在 SharedPpreferences 文件中。在你的onLongClickListener中这样做

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

然后在创建之前创建,然后使 Toast 将其加载到 fa 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();
}

最新更新