favoritekafe
にfMessage
を保存し、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;
}
});
}
}
よう
favoritekafe
は、なぜあなたはそれを返すようにしたいんFAにロード行う前のonCreateで、この
しますか?あなたはそれを 'favoritekafe = fMessage;'と同じにすることができ、onItemLongClick()は常にtrue/falseを返します。 – Adithya
私はこの活動が乾杯するたびに欲しいから。 以前Listviewからアイテムを長時間クリックしたかどうかによって異なります。 (私の英語は申し訳ありません、それは私の母国語ではありません) –