0

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.

Jad
  • 479
  • 2
  • 10
  • 23
Test123
  • 368
  • 1
  • 7
  • 26

4 Answers4

1

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.

  • @Sonam Did you try out my solution yet? Do you understand my solution? –  Nov 26 '14 at 13:12
0

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

Forin
  • 1,549
  • 2
  • 20
  • 44
0

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, "");
    }
devcelebi
  • 878
  • 7
  • 11
0

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>());
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74