barmi
barmi

Reputation: 665

Android List in SharedPreference

I am trying to edit values from List in SharedPreferences but someting is going wrong.

My SharedPreference is:

public class StepsData {
    static SharedPreferences data;
    static SharedPreferences.Editor editor;
    static final int VALUE_KEY = 0;
    static final List<String> LIST_KEY= new Vector<String>();
}

I use SharedPref by:

StepsData.data = getApplicationContext().getSharedPreferences("userData", MODE_PRIVATE);
StepsData.editor = StepsData.data.edit();

if I want to edit or get value from VALUE_KEY everything works by:

int step = StepsData.data.getInt(String.valueOf(VALUE_KEY), 0);
editor.putInt(String.valueOf(VALUE_KEY), 0).apply();

but I have a problem with using List, my code for get value is:

List<String> myList = (List<String>) data.getStringSet(String.valueOf(LIST_KEY),null);

and for delete:

List<String> clearList = new Vector<String>();
editor.putStringSet(String.valueOf(LIST_KEY), (Set<String>) clearList).apply();

but there is a NullPointerException. What is the best way to use something like ".clear()" on List from SharedPreference and how is is possible to get values from this List and size?

Upvotes: 2

Views: 159

Answers (3)

sushildlh
sushildlh

Reputation: 9056

The the following:

Declare myList globally:

ArrayList<String> myList = new ArrayList<String>();

For setting value:

for (int i = 0; i < totalSize; i++) {
    PreferenceManager.getDefaultSharedPreferences(this)
                     .edit()
                     .putString("number" + i, value + "").commit();         
}

For getting value:

for (int i = 0; i < totalSize; i++) {
   myList.add(PreferenceManager.getDefaultSharedPreferences(this)
                               .getString("number" + i, "0"));
}

NOTE:- totalSize is the size of your array

Upvotes: 1

chetan
chetan

Reputation: 681

if you want to store List object in SharedPreference then use gson library. It will use to convert list object into json format and store that json string into sharedPref.
First include this line in gradle file(app level)

compile 'com.google.code.gson:gson:2.4'

below code is to set the Type to List

Type listType = new TypeToken<List<String>>(){}.getType();

Gson gson = new Gson();

Now Create list object and convert to json string format using gson object and type

List<String> myList = new ArrayList<>();
//add elements in myList object

String jsonFormat = gson.toJson(myList,listType);  
//adding to sharedPref  
editor.put("list",jsonFormat).apply();

Now get the values from sharedPref and convert json string back to List object.

//this line will get the json string from sharedPref and will converted into object of type list(specified in listType object) 
List<String> list = gson.fromJson(sharedPref.get("list",""),listType);  
//now modify the list object as par your requirement and again do the same step of converting that list object into jsonFormat.

Upvotes: 4

Mithat Konuk
Mithat Konuk

Reputation: 457

why do you using list ?(which is contains duplicate element)
getting all element from sharedpreference

Set<String> set = preference.getStringSet("key", null);

//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
preferenceEditor.putStringSet("key", set);
preferenceEditor.commit();

if you must use list check this link also https://gist.github.com/cr5315/6200903

Upvotes: 1

Related Questions