I want to store the fMessage
to the variable favoritekafe
and return the favoritekafe
after the onItemLongClick
method ends.
Is that possible in any way?
OR can i accomplish with a different way to return a variable except true/false from "onItemLongClick" method?
Thats my code:
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 :(得分:1)
Create a function inside your class
void gotStringFromLongClick(String fMessage){
favoritekafe=fMessage;
// do anything else you want here
}
and call it like gotStringFromLongClick(fMessage);
from outside 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();
Retrieve value in 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();
}
答案 1 :(得分:0)
您可以将其存储在SharedPreference文件中。 在你的onLongClickListener里面做这个
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("fav_kafe", favoritekafe).commit();
然后在onCreate中,然后将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();
}