Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can write code to make sharedpreferences for array in android?

Tags:

android

I am working in android. I want to make sharedpreference in my code, but i dont know the way by which i can make a sharedpreference for array and how can use the value of that sharedpreference in another class.

This is my array in one for loop :- urls[i]=sitesList.getWebsite().get(i);

i want to make share preference of this urls[] array. please suggest me how can i write code to declare sharedpreference and how can i retrieve value of that sharedpreference ?

Thank you in advance.

like image 735
Pushpendra Kuntal Avatar asked Sep 09 '11 12:09

Pushpendra Kuntal


People also ask

Can we store array in SharedPreferences?

You can save String and custom array list using Gson library. =>First you need to create function to save array list to SharedPreferences. public void saveListInLocal(ArrayList<String> list, String key) { SharedPreferences prefs = getSharedPreferences("AppName", Context. MODE_PRIVATE); SharedPreferences.

How do I save an ArrayList of custom objects to SharedPreferences?

SharedPreferences appSharedPrefs = PreferenceManager . getDefaultSharedPreferences(this. getApplicationContext()); Gson gson = new Gson(); String json = appSharedPrefs. getString("MyObject", ""); Student mStudentObject = gson.

How do I write to SharedPreferences?

Write to shared preferences To write to a shared preferences file, create a SharedPreferences. Editor by calling edit() on your SharedPreferences . Note: You can edit shared preferences in a more secure way by calling the edit() method on an EncryptedSharedPreferences object instead of on a SharedPreferences object.


1 Answers

putStringSet and getStringSet are only available in API 11.

Alternatively you could serialize your arrays using JSON like so:

public static void setStringArrayPref(Context context, String key, ArrayList<String> values) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    JSONArray a = new JSONArray();
    for (int i = 0; i < values.size(); i++) {
        a.put(values.get(i));
    }
    if (!values.isEmpty()) {
        editor.putString(key, a.toString());
    } else {
        editor.putString(key, null);
    }
    editor.commit();
}

public static ArrayList<String> getStringArrayPref(Context context, String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String json = prefs.getString(key, null);
    ArrayList<String> urls = new ArrayList<String>();
    if (json != null) {
        try {
            JSONArray a = new JSONArray(json);
            for (int i = 0; i < a.length(); i++) {
                String url = a.optString(i);
                urls.add(url);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return urls;
}

Set and retreive your URLs like so:

// store preference
ArrayList<String> list = new ArrayList<String>(Arrays.asList(urls));
setStringArrayPref(this, "urls", list);

// retrieve preference
list = getStringArrayPref(this, "urls");
urls = (String[]) list.toArray();
like image 147
Jeff Gilfelt Avatar answered Oct 07 '22 01:10

Jeff Gilfelt