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

4 Answers4

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
  • 437
  • 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 
    }
-1

you can add, update or delete shared preference on anytime, it it will be more easier if you use sharedpreference.

/******* Create SharedPreferences *******/

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit();

/**************** Storing data as KEY/VALUE pair *******************/

editor.putBoolean("key_name1", true);           // Saving boolean - true/false
editor.putInt("key_name2", "int value");        // Saving integer
editor.putFloat("key_name3", "float value");    // Saving float
editor.putLong("key_name4", "long value");      // Saving long
editor.putString("key_name5", "string value");  // Saving string

// Save the changes in SharedPreferences
editor.commit(); // commit changes

/**************** Get SharedPreferences data *******************/

// If value for key not exist then return second param value - In this case null

pref.getBoolean("key_name1", null);         // getting boolean
pref.getInt("key_name2", null);             // getting Integer
pref.getFloat("key_name3", null);           // getting Float
pref.getLong("key_name4", null);            // getting Long
pref.getString("key_name5", null);          // getting String

/************ Deleting Key value from SharedPreferences *****************/

editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4

// Save the changes in SharedPreferences
editor.commit(); // commit changes

/************ Clear all data from SharedPreferences *****************/

 editor.clear();
 editor.commit(); // commit changes
Mr Robot
  • 3,341
  • 7
  • 39
  • 70