简体   繁体   中英

How to store array in an sharedpreferences in android

I am building an android application where the user selects his event from spinner tool.

The Spinner tool displays the array list that user had selected first time the application is launched.

Now I had parsed the array list from app launch page to spinner activity class an using it in spinner tool success full now.

Here is code:

public static ArrayList<String> array;

Here, the name array has the arraylist. I need to store this in sharedprefrences. How can I do this ?

I am new to android.

I don't suggest using putStringSet() since it will screw up your arrayorder.

I suggest running a for loop over your array and giving them different key names. + in the end adding another String, that tells you how long the array is once you wanna read out the strings of the SharedPreferences.

General settings:

 SharedPreferences sharedPreferences = getSharedPreferences("YOURKEYFILE", Activity.MODE_PRIVATE); SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); 

Save String Array:

 for (int i = 0; i < array.size(); i++) { sharedPreferencesEditor.putString("StringArrayElement" +i, array.get(i)); } sharedPreferencesEditor.putInt("StringArrayLength", array.size()); sharedPreferencesEditor.commit(); 

Read String Array:

 array.clear(); for (int i = 0; i < sharedPreferencesEditor.getInt("StringArrayLength", 0) { array.add(sharedPreferencesEditor.getString("StringArrayElement" +i, ""); } 

NOTE: The above code is untested! Community correct me there if you find mistakes.

You can save your array as Serializable object.

//save the task list to preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
try {
    editor.putString(ARRAY, ObjectSerializer.serialize(array));
} catch (IOException e) {
    e.printStackTrace();
}
editor.commit();

And to retrieve it from SharedPreferences:

SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);

try {
    array = (ArrayList<String>) ObjectSerializer.deserialize(prefs.getString(ARRAY,
         ObjectSerializer.serialize(new ArrayList<String>())));
} catch (IOException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

You can get ObjectSerializer.java from here

This code help to you maybe.You save list item by counter i.

for(int i=0;i<list.size();i++)
    {
editor.putString(list.get(i)+i, list.get(i));
editor.commit();}
for(int i=0;i<list.size();i++)
    {
preferences.getString(list.get(i)+i, "");
    }

It's better to go with Set<String> as API version 11 introduced methods putStringSet and getStringSet which allows the developer to store a list of string values and retrieve a list of string values, respectively.

Save list

// Save the list.
editor.putStringSet("array", myStrings);
editor.commit();

Fetch list

// Get the current list.
SharedPreferences settings = this.getSharedPreferences("YourActivityPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
Set<String> myStrings = settings.getStringSet("array", new HashSet<String>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM