开发者

How do I save history in an android app?

开发者 https://www.devze.com 2023-04-03 07:04 出处:网络
I have a listview called history that I call every time that I so something significant (ie open or close the app or record some info) The problem I have is that it records things from top to bottom m

I have a listview called history that I call every time that I so something significant (ie open or close the app or record some info) The problem I have is that it records things from top to bottom meaning the most recent information is on the bottom of the listview. The second problem开发者_开发技巧 I have is that the listview gets deleted after the app is turned off. How do people usually save their history? (ie shared preferences can only store one variable and thats not enough, I would like to save a maximum of 30 entries to the listview)

Thanks


One option I use is to JSON encode my history list and store the resulting string to SharedPreferences. You can then order it as well (simply add items to the List that backs the ListView with new events first) and save it and restore it from the SharedPreferences file.

EDIT: Try something like this:

org.json.JSONArray tracking_users = new org.json.JSONArray();
tracking_users.put("history 1");
tracking_users.put("history 2");
tracking_users.put("history 3");


You could also consider using a database to store the Log of these events, tends to be more efficient.

See one of these for an introduction to android databases

0

精彩评论

暂无评论...
验证码 换一张
取 消