[英]Save new form of custom ArrayList, SharedPreferences
Noobie在這里。 我有一個問題和答案的custum ArrayAdapter。 當一個列表項被longClicked時,它將被刪除。 現在,我想使用刪除的項目保存ArrayList的新狀態,因此,如果您轉到應用程序中的另一個選項卡,然后返回,則只剩下其余的問題。 我在網上看了看,發現看起來像我需要的東西,但是我看不到為什么它不起作用。
public class OnePointQuestion extends AppCompatActivity {
ArrayList<Question> TheQuestion;
String key = "ABCD";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
if(TheQuestion == null){
createList();
}else{
getArrayList(key);
}
final MyAdapter adapter = new MyAdapter(this, TheQuestion);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
//When the list item is clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Identify the current question
Question currentQuestion = TheQuestion.get(position);
//Send the answer to a second page to be shown
Intent i = new Intent(OnePointQuestion.this, TheAnswer.class);
i.putExtra("TheAnswer", currentQuestion.getAnswer());
startActivity(i);
}
});
//When the list item is long clicked
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Identify the current question
Question currentQuestion = TheQuestion.get(position);
//Remove this element of the list
TheQuestion.remove(currentQuestion);
adapter.notifyDataSetChanged();
Toast.makeText(OnePointQuestion.this, "Urmatoarea intrebare", Toast.LENGTH_SHORT).show();
return true;
}
});
}
//Save the ArrayList
public void saveArrayList(ArrayList<Question> list, String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OnePointQuestion.this);
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString(key, json);
editor.apply(); // This line is IMPORTANT !!!
Toast.makeText(OnePointQuestion.this, "Saved", Toast.LENGTH_SHORT).show();
}
//Load the ArrayList
public ArrayList<Question> getArrayList(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OnePointQuestion.this);
Gson gson = new Gson();
String json = prefs.getString(key, null);
Type type = new TypeToken<ArrayList<Question>>() {}.getType();
Toast.makeText(OnePointQuestion.this, "Loaded", Toast.LENGTH_SHORT).show();
return gson.fromJson(json, type);
}
//Load the ArrayList when the app gets to the OnStart
@Override
protected void onStart() {
super.onStart();
getArrayList(key);
}
@Override
protected void onPause() {
super.onPause();
saveArrayList(TheQuestion, key);
}
public void createList(){
//Create the list of questions
TheQuestion = new ArrayList<>();
TheQuestion.add(new Question("Cum te cheama?", "Irelevant"));
TheQuestion.add(new Question("Cati ani ai?", "Prea multi"));
TheQuestion.add(new Question("De ce ai dat la Poli", "Asta ma intreb si eu"));
}
}
我正在嘗試使用的“保存”和“加載”方法即將結束。 如果您能告訴我我在做什么錯,我將不勝感激,謝謝:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.