英文:
How to put in memory a ListView and access it when the app is started again?
问题
我有一个带有自定义对象的 ListView,我不知道如何将其项目保留在内存中,这样每次打开应用程序时,ListView 都将拥有其项目。
英文:
I have a ListView with custom objects and I don't know how to keep its items in memory, so every time the app is open the ListView will have its items.
答案1
得分: 0
你可以使用SharedPreferences来实现这个。例如:
public void storeListValues(String values) {
// 首先检索SharedPreferences
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(填入你的应用程序名称, MODE_PRIVATE);
// 将你的值存入内存
sharedPreferences.edit().putString("listValues", values).apply();
}
你需要将项目以某种方式转换为字符串,例如使用逗号分隔。
英文:
You could use the SharedPreferences for this. For example:
public void storeListValues(String values) {
// First retrieve the SharedPreferences
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(FILL_IN_YOUR_APP_NAME, MODE_PRIVATE);
// And put your values into memory
sharedPreferences.edit().putString("listValues", values).apply();
}
You'd have to convert the items into a String somehow, for example using comma separation.
专注分享java语言的经验与见解,让所有开发者获益!
评论