我有一个字符串 ArrayList,我正在从中绘制一个 ListView。在应用程序中,用户可以将项目添加到 ArrayList,从而使项目显示在 ListView 上。当应用程序正在运行时,ArrayList 保留其项目,但在重新启动后(或应用程序停止的任何其他时间)项目将丢失。我读过有关内部、外部和 SQLite 存储的信息,但我无法使内部或外部存储正常工作,而且我对 SQLite 一无所知,该应用程序将于周五到期。有没有更好的实现saving?我已附上完整的代码副本,没有任何存储方法以供审核。
谢谢。
最佳答案
使用首选项实现来保存 ArrayList<String>
.在onPause
方法使用这个:
ArrayList<String> tobesaved = getData(); // fetch the data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor edit = prefs.edit();
edit.putStringSet("SAVEDATA", new HashSet<String>(tobesaved));
edit.commit();
然后你可以在 onResume
中获取它方法:
ArrayList<String> retrieved = new ArrayList<String>(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getStringSet("SAVEDATA", new HashSet<String>()));
编辑:我没有机会查看您的代码:mediafire 试图在我的浏览器上打开一些弹出窗口,所以我迅速关闭了该选项卡,但这应该对您有用。尝试并可能使用 gist(位于 http://gist.github.com)或 pastebin(位于 http://pastebin.com/)来共享更长的代码序列。
关于android - 应用关闭后在 Android 中存储 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6133251/