StringSets won't save/load to/from SharedPreferences - android

My app stores an ArrayList of custom Food objects which are posted to a ListView in the main Activity. The ListView portion of the app works perfectly, but I'm having an issue saving and loading these Food objects to and from SharedPreferences.
I'm saving these Food objects to SharedPreferences by saving each of its attributes as a String, putting them in a StringSet, and saving each one via the putStringSet function. When the app resumes, it reads through each StringSet from SharedPreferences and creates a new Food item with the read attributes, then adds each one to the ArrayList. The code seems like it should work, but just doesn't load the data between sessions. Can anyone help me figure out why? Thank you!
Code removed

You seem to never instantiate foodNamesSet variable. Hence the check if(foodNamesSet != null) always return false. Same thing applies to other sets as well. Besides, I think you should do the logic in onPause() rather in onStop().

Related

How to make a dynamic SharedPreferences list that works with arrays and ArrayAdapters?

I've found several similar pages that haven't quite been able to address my dilemna. Here goes:
I would like to be able to create a SharedPreferences list in Android that is extractable with an iterator (loop). Somehow I need to be able to add to a list with a key-value of "[Arrayname]+[element_number]" format. I also need to be able to add/remove values at will.
-- How will I be able to add to the end if I don't know how long the current SharedPrefs list is?
-- I also need the length of the SharedPrefs list to create arrays from it.
You get the a map of all values contained into a SharedPreferences instance with the getAll() method. You can then convert it to a list.
I suggest working with a regular in data container (e.g. ArrayList or HashMap).
you can store the container in the shared preference by serializing it:
https://stackoverflow.com/a/5816861/1393632
Now you just need to keep it in sync with your shared preference when updating it.

Preference Activity store/load values from custom source

I am building an app which allows the user to define multiple objects of a specific type, lets call them "Person"'s. The "Person" object is defined as such:
class Person {
public String name;
public int age;
}
These "Person" objects will be serialised and stored within the app, either in SharedPreferences or via a Cloud storage mechanism. This can be considered handled and working.
My problem is that I need an editor interface to allow the user to change the "name" and "age" of any particular "Person" instance in their collection. In order to make my UI feel as much like stock as possible, I would like my editor interface to resemble the "Preference" interface which Android implements. As such I need a way to make a "PreferenceActivity" load and save it's preferences from/to a POJO.
The reason I am looking to do this is so that I have a UI that feels like something the user is used to using. My other choice is to mimic the style and create all the handling code myself, which will take a lot of time.
I imagined the process would be to override the "load" and "save" functions of a "PreferenceActivity" to pull/push the values from a POJO provided "onCreate" via an "Intent" - and the return this POJO as an activity result to the caller.
Is this achievable?
What is the purpose of having it this way? I fail to see why this would be useful nor how it would be feasible with regards to the reference of the pojo containing the data. If it was possible then the saving object would most likely be a generic key-object map, where you would need to extract the data from, which is exactly how the Preferences already work.
If the point of this is just to have the information in a Person object why not just make a method that creates one based on the saved preferences.
----- Additions
If you add a static/singleton data handler(repository) in the App that will contain all the persons while the App is executing, and that it has some kind of identifier for each person. Then you can pass the ID in the intent to the PreferenceActivity which will in turn fetch the person object from the data handler and fill in the values of the PreferenceActivity based on it.
Add another Preference to the PreferenceActivity named "Save" or similar, which you resolve and bind in the activity. When clicked this will fetch the currently entered information (which will be saved in the SharedPreferences) and create a Person instance out of it. It should then pass this object to the data handler which will add it (or update it if the ID is already there) to the repository of Persons. At this time you should probably consider serializing the whole repository and save it, one easy way is just to JSON it all and put it into the SharedPreferences. Don't forget that you need to load this data the first time you access the data handler so that the previously saved persons are accessible.
I would also recommend you create Interfaces for the data handling action in case you want to add new or replace the implementation to for example database operations instead.
This way you can use the PreferenceActivity for add/editing Persons. Even though I would prefer create your own UI for it.

Save state of checkbox in single choice list while navigating to another activity

How can i Save state of checkbox in single choice list while navigating to another activity & come back to previous activity. Any code snippet would be appreciated. Thanx in advance
Just save the value in SharedPreferences.
Reference is here: http://developer.android.com/reference/android/content/SharedPreferences.html
Example here: http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html
You will want to save the current value each time it changes in a shared preference value.
and have initMethod called in onCreate() and perhaps onStart() as well that checks the to see if the value is set in the SharedPreferences and if so initialize to that value.
There are numerous tutorials on SharedPreferences, they are definitely the place to store this kind of data. Otherwise you would need to subclass the Application object which is not a good idea.
There are a couple of things you may want to do.
#1 Pass data into another activity
intent.putExtra("keyName", "somevalue");
We can add multiple entries here. This is a key,value pair. So to receive this data from the receiving activity we have to write this code
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
Read more: http://getablogger.blogspot.com/2008/01/android-pass-data-to-activity.html#ixzz24FoEOTwH
#2 Using Shared Preferences
Tons of info on this.
Easiest way to store data in Android.
http://developer.android.com/guide/topics/data/data-storage.html#pref
http://www.slideshare.net/androidstream/sharedpreferences-tutorial
#3 Using SQLite DB
Databases are great, might be going to far for just saving a checkmark
http://developer.android.com/guide/topics/data/data-storage.html#db

How can I save the state of the checkboxes in a custom listview using sharedpreferences?

I have checkboxes in my custom listview, I'm using a boolean array to save the state of these checkboxes. I want to make the state of checkboxes persistent through out the lifetime of the app.I know that this can be achieved through sharedpreferences, but I don't exactly know how this can be done.
I know that this can be achieved through sharedpreferences, but I don't exactly know how this can be done.
There is no option to push serializable objects into sharedpreferences. Because of that, you'll be forced to convert the boolean array to one of the supported types. The only one I can see making sense would be to convert the state of the array into a string like :
"0|1|0|1|1"
Then push that into the shared preferences. To do this you could use the Arrays.toString(boolean []). You will, however, have to write a parse method for extracting the value back out from the SharedPreferences. That is probably the easiest option to accomplish this.

How to store and retrieve a dynamic listview

I Have 2 activities: activity X & activity Y.
Y sends data to X via an intent and X displays the data in a listView.
The problem I am having is saving the listView in X so the next time a user goes to activity Y and sends an intent the listView will display the last data that was send + the new data, this way the user will continue to populate the ListView .
I was thinking about saving all the data in a sqlLite database and then retrieving it and displaying the updated listView that way?
or maybe to serialize my list and save it via SharedPrefs (not sure if that would actually work,I am a really new at this)
Any suggestions and code samples would be appreciated!!!! THANK YOU!
If you have a small amount of data (<5 variables) I suggest you to implement a Sharepreferences class with the methods you need (basically, put and get). For more than that, someone told me to use SQLite, but I've never used before, even if it seems easy to put in place.
Besides, these are methods useful if you want to store data even if the app is closed, and you could retrieve them after in another session. If data lives only for a session, put everything in a bundle and go back and forth with it.
well, You can use any of the
1) sharedPreference : for primitive data only .
2)File storage(internal/external) : limited size , no quert support , suitable when storing long string kind of data
3)SQlite : suitable for complex structure , because of query support

Categories

Resources