且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用共享首选项保存和重新加载 ListView [保存 onDestroy()]

更新时间:2023-02-02 20:57:27

Android SharedPreferences 用于存储单个键值对数据.换句话说,它不是用来存储重复和复杂的数据.在您的情况下,列表视图可能包含 100 行或更多行,具体取决于您的应用.如果您要为每一行创建一个 SharedPreferences 对象,它将是 100+,这根本没有效率.对此的解决方案如上所述,是使用 Android 的 SQLite 数据库.

Android SharedPreferences is used to store a single key-value paired data. In other words, it is not made to store repetitive and complex data. In your case, a list view may contain than 100 rows or more depends on your app. If you were to create a SharedPreferences object for each row it would be 100+ of it, which is not efficient at all. The solution for this is as suggested above, is to use the Android's SQLite database.

一个很好的教程:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/