Professional Documents
Culture Documents
Questions
Tags
Users
sign up
Badges
Unanswered
log in
Ask
android
listview
android-listview
save
I am creating a To Do List. Right now my app adds and removes data to and from a ListView. However, when I close my application the data
gets erased. I want to use SharedPreferences to save the data onDestroy(), then when my app opens I wanted the data to reload.
However, I don't know how to go about accomplishing this task. Can anybody help me with saving a ListView using Shared Preferences (aka I
am looking for code)?
There are tutorials for just one shared preference I am looking to dynamically add multiple strings when my application closes. Then when I
reopen it, the data will appear. I am only using ONE ACTIVITY page, everything will occur on one page.
HERE IS MY CODE:
share
Asked
Sep 9 '13 at 17:02
add a comment
3 Answers
order by votes
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.
share
Answered
Mar 4 '14 at 16:36
Some considerations:
Save SharedPreferences in onPause() rather than in onDestroy() which is not guaranteed to be called. See Activity Lifecycle for
reference.
Forbid user from inputing an empty String. (Edit: You are already doing that.)
If you know there won't be 2 identical String (forbidden or otherwise), you can use the SharedPreferences.Editor.putStringSet(String,
Set<String>) to use a single entry.
If there is a lot of elements or if you're planning on adding extra options (i.e. Due date, Category, etc.) You should really consider
the database options since the SharedPreferences solution doesn't provide good scalability.
share
Answered
Sep 9 '13 at 18:34
Edited
Sep 10 '13 at 9:48
add a comment
I know its old but i want to share my solution for those who Still want to use sharepref and see this post.
1) create custom class that extends ArrayAdapter and override the toString method.just append and put ',' between every line.
textViewResourceId) {
objects) {
3)In onResume restore the data back by splinting the string with ',' and add it to the adapter.
share
Your Answer
log in
Answered
May 22 '15 at 0:32
or
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc