2

In android app I have list view where you can add new objects, each 'object' have 4-5 string values. I dont predict anyone could use more 3 or 4 of those objects in app.

As for now its made on database, SQLite, one object = one record, with 4-5 values (type TEXT), but its getting harder and harder to maintain, and I think its adding to app circuitous.

Could it be done by shared preferences ? Or its not good idea to store such data ? How about keys and values, can i generete them on go ?

Jack Jason F
  • 143
  • 1
  • 3
  • 12
  • 1
    No, shared preferences cannot be used to store larger objects, its a bad thing to do so. [Developer site](http://developer.android.com/guide/topics/data/data-storage.html#pref) – codename_47 Feb 29 '16 at 07:09

3 Answers3

1

You will need Gson to put object in Shared Preferences. You can find it here and how to add it to your project like this. Don't forget to add dependency in gradle file 'build.gradle' compile files ('libs/gson-2.2.4.jar').

To put object in Shared Preferences use the following:

SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String serializedObj = gson.toJson(ObjectHere);
editor.putString("key", serializedObj);
editor.commit();

To retrieve the data later do the following

 SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
    Gson gson = new Gson();
    String serializedObj = preferences.getString("key", "DEFAULT");
    ObjectType object = gson.fromJson(serializedObj, Object.class);
Community
  • 1
  • 1
anaxin
  • 710
  • 2
  • 7
  • 16
0

You can write the data as a file and retrieve it when needed using the filename:

    String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

It would be better if u perform this task in a background thread for instance an AsyncTask.

codename_47
  • 449
  • 2
  • 17
0

It can be solved using gson(https://github.com/google/gson). Simply add compile files ('libs/gson-2.2.4.jar') on your gradle dependencies section.

Suppose you are using Arraylist of class YOUR_CLASS for listview. ie

private ArrayList<YOUR_CLASS> arraylist = new ArrayList<YOUR_CLASS>();

Now after adding item objects to your arraylist you can save it in Shared preference using:

SharedPreferences preferences = getSharedPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

Gson gson = new Gson();
String arraylist_in_string = gson.toJson(arraylist).toString(); //Converting it to String
editor.putString("YOUR_KEY", arraylist_in_string); //now saving the String
editor.commit();

Now when you need to use the arraylist you can take it from sharedPreferences using

     Gson gson = new Gson();
        String arraylist_in_string = preferences.getString("YOUR_KEY", null);
    if(arraylist_in_string != null) //if we have list saved
    { 
//creating a list of YOUR_CLASS from the string response
        Type type = new TypeToken<ArrayList<YOUR_CLASS>>() {}.getType();
        List<YOUR_CLASS> list = new Gson().fromJson(arraylist_in_string, type);
//now adding the list to your arraylist
    if (list != null && list.size() > 0 )) {
    arraylist.clear();// before clearing check if its not null. 
                    arraylist.addAll(list);
    }else //ie list isn't saved yet
    {
    //You may want to save the arraylist into Shared preference using previous method 
    }