1

Possible Duplicate:
Is it possible to add an array or object to SharedPreferences on Android

i created multiple alarm in my alarm clock using shared preferences now i want the alarm should ring at particular day of the week, for that i want an array which can save he day on which alarm should ring. can anyone tell how to put array in shared preferences and how to retrive it again..?

SharedPreferences set=getSharedPreferences(MYPR, 0);
            SharedPreferences.Editor ed=set.edit();
            ed.putInt("Ahh", Ahh);
            ed.putInt("Amm",Amm);
            ed.putBoolean("en", true);
            ed.commit();

now i am putting alarm time and its property in shared pref. what is to be added to put array (day of week)also..?

Community
  • 1
  • 1
Scorpian
  • 113
  • 1
  • 1
  • 9

1 Answers1

5

To write,

SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    JSONArray jsonArray = new JSONArray();
    jsonArray.put(1);
    jsonArray.put(2);
    Editor editor = prefs.edit();
    editor.putString("key", jsonArray.toString());
    System.out.println(jsonArray.toString());
    editor.commit();

To Read,

try {
        JSONArray jsonArray2 = new JSONArray(prefs.getString("key", "[]"));
        for (int i = 0; i < jsonArray2.length(); i++) {
             Log.d("your JSON Array", jsonArray2.getInt(i)+"");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
kamal
  • 290
  • 3
  • 20