如何将ListView存入内存并在应用程序再次启动时访问它?

huangapple 未分类评论54阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月7日 22:13:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61082052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定