java - 保存新形式的自定义 ArrayList、SharedPreferences
问题描述
菜鸟在这里。我有一个带有问题和答案的自定义 ArrayAdapter。当一个列表项被长按时,它会被删除。现在我想用删除的项目保存 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"));
}
}
我尝试使用的“保存”和“加载”方法已接近尾声。如果你能告诉我我做错了什么,我将不胜感激,谢谢:)
解决方案
推荐阅读
- mongodb - 如何仅基于日期而不是时间戳获取过滤 mongodb 文档?
- xml - 如何解决此“Cvc-elt.1.a:找不到元素'soap:Envelope'的声明。,'2'行,'63'列”
- angular - 为什么在尝试使用 Angular 检索数据库信息时出现错误 415?
- java - Java Iterator - 不能从静态上下文引用非静态方法 getID()
- c# - Uri Path 与 Java.IO.File 不兼容
- python - 如何在 Python 的 print 语句中获取准确克隆字符的空间?
- node.js - Sequelize (PostgresSQL) 多对多聊天实现
- c++ - 如何从 DirectShow 获取未压缩的字节数组?
- c# - 两个相同的类方法,具有不同的访问修饰符和接口
- powershell - 启动过程:位置参数和找不到指定的文件