使用onItemLongClick(Android)后存储字符串消息
我想将使用onItemLongClick(Android)后存储字符串消息,android,Android,我想将fMessage存储到变量favoritekafe中,并在onItemLongClick方法结束后返回favoritekafe 这有可能吗 或者,我可以用不同的方法从“onItemLongClick”方法返回除true/false之外的变量吗 这是我的密码: import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.w
fMessage
存储到变量favoritekafe
中,并在onItemLongClick
方法结束后返回favoritekafe
这有可能吗
或者,我可以用不同的方法从“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;
}
});
}
}
导入android.support.v7.app.app活动;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.content.SharedReferences;
公共类MainActivity2扩展了AppCompatActivity{
ListView-kafeteries;
字符串偏爱卡夫;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.coffee);
if(favoritekafe==null){
Toast.makeText(getApplicationContext(),“您没有收藏夹
“咖啡还没有”,吐司。长度(短)。show();
}
否则{
Toast.makeText(getApplicationContext(),favoritekafe,
吐司。长度(短)。show();
}
/*伊卡费特丽斯塔酒店*/
kafeteries=(列表视图)findViewById(R.id.kafeteries);
ArrayAdapter mAdapter=新的ArrayAdapter
(主要活动2.这一点,
android.R.layout.simple\u list\u item\u 1,
getResources().getStringArray(R.array.kafeteries_syros));
kafeteries.setAdapter(mAdapter);
//mnmta TOAST otan kanw click kapoio stixeio tis listas*/
kafeteries.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串sMessage=“”;
开关(位置){
案例0:sMessage=“与热情好客的当地人一起喝咖啡和饮料\nArea:Ermoupoli\n电话:2281083354”休息;
案例1:sMessage=“城市狭窄街道上的咖啡\n区域:Ermoupoli\n电话:2281079225”中断;
案例2:sMessage=“镇上最小最可爱的咖啡\n地区:Ermoupoli\n电话:2281300880”休息;
案例3:sMessage=“城市港口的咖啡和小吃\nArea:Ermoupoli\n电话:2281076144”休息;
案例4:sMessage=“该市最著名的咖啡馆\nArea:Ermoupoli\n电话:2281085337”中断;
}
Toast.makeText(getApplicationContext(),sMessage,Toast.LENGTH_LONG).show();
}
});
//prospathia gia long clik添加最喜爱的kai save*/
kafeteries.setLongClickable(true);
kafeteries.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView arg0、视图arg1、整型位置、长id){
//准备好了一份报告,将其提交给阿加皮米尼医生*/
字符串fMessage=“”;
开关(位置){
案例0:fMessage=“Boheme del mar是你最喜欢的咖啡馆”休息;
案例1:fMessage=“Jar是最喜欢的咖啡馆”中断;
案例2:fMessage=“Kouchico是你最喜欢的咖啡馆”休息;
案例3:fMessage=“Okio是你最喜欢的咖啡馆”休息;
案例4:fMessage=“Plaza是你最喜欢的咖啡馆”休息;
}
Toast.makeText(getApplicationContext(),fMessage,Toast.LENGTH_SHORT).show();
最终SharedReferences prefs=getApplicationContext().GetSharedReferences(“设置”,模式\私有);
prefs.edit().putString(favoritekafe,fMessage.commit();
返回true;
}
});
}
}
在类中创建函数
void gotStringFromLongClick(String fMessage){
favoritekafe=fMessage;
// do anything else you want here
}
并将其命名为gotStringFromLongClick(fMessage)代码>从外部开关
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();
在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();
}
您可以将其存储在SharedReference文件中。
在您的onLongClickListener中执行此操作
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("fav_kafe", favoritekafe).commit();
然后在制作Toast之前,在onCreate中将其加载到fafavoritekafe
像这样
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();
}
你为什么要退货?您可以将其等同于favoritekafe=fMessage
和onItemLongClick()将始终返回true/false。因为我希望每次此活动开始时都有一个祝酒词。取决于您以前是否长时间单击列表视图中的项目。(对不起,我的英语不是我的母语)它似乎不起作用。每次我开始这个活动时,“favoritekafe”都是空的。很明显,开始时,你有一个值,尽管你可以在SharedReference
中保留值,但它起作用了。。。!!!只有当“favoritekafe”为空时,toast才会显示为空。