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.

6
  • Serialize the array .. Commented Nov 18, 2014 at 9:33
  • @MurtazaHussain How can I do.. Commented Nov 18, 2014 at 9:36
  • @Sonam Check this link stackoverflow.com/questions/7057845/… Commented Nov 18, 2014 at 9:39
  • @SweetWisherツ how ?? Commented Nov 18, 2014 at 9:59
  • have a look here Commented Nov 18, 2014 at 10:01

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Sonam Did you try out my solution yet? Do you understand my solution?
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

7 Comments

In ObjectSerializer. java class error in WrappedIOException -WrappedIOException cannot be resolved.. please help me
Have you imported it ??
@Sonam For other solution you can convert arraylist to String array and save that array to SP and retrieve it easily too.!!
@PiyushGupta I used it but when I am Toasting the value save in SharedPreferences there is null toasting in it.
i m getting same error @PiyushGupta mentioned over here working with custom objects how can i overcome in this situation ?
|
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, "");
    }

Comments

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>());

5 Comments

Here is showing an error on putString - The method putStringSet(String, Set<String>) in the type SharedPreferences.Editor is not applicable for the arguments (String, ArrayList<String>). please help to resolving it
You need to use Set<String> instead of ArrayList<String>
But I am parsing the value from firstactivity to other so i am using ArrayList<String> so please tell me alter way to do this
Can i store the value of ArrayList<String> to Set<String> in same activity
Yes you can. ..please check the android developer site to work with set or refer java tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.