0

how can I use this code in order to add an array and retrieve it later? can I use a simple for loop?

  SharedPreferences settings = getSharedPreferences("isChk", 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("keys",ArrayValue[i] );
  editor.commit();

and how can I then retrieve them and store each element in an array variable can I use this?

for( int i=0; i<myArr.size(); i++){
keys[i] = settings.getBoolean("isChk", false);
isChkb.add(keys[i]);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

Here is how u do to save Array in Shared Preferences

public static boolean saveTheArray()
{
 SharedPreferences sharedpref = SharedPreferences.getDefaultSharedPreferences(this);
 SharedPreferences.Editor mEdit1 = sharedpref .edit();
 mEdit1.putInt("Status_size", array.size()); //array-> the array to be saved 

for(int i=0;i<array.size();i++)  
{
    mEdit1.remove("Status_" + i);
    mEdit1.putString("Status_" + i, array.get(i));  
}

return mEdit1.commit();     

}

To retriev it use the following code:

public static void loadArray(Context mContext)
{  
SharedPreferences mShrdPref = PreferenceManager.getDefaultSharedPreferences(mContext);
array.clear();
int size = mShrdPref.getInt("Status_size", 0);  

for(int i=0;i<size;i++) 
{
    array.add(mShrdPref.getString("Status_" + i, null));
}
}

Hope it helps :)

ik024
  • 156
  • 3
  • 14
  • and in order to get retrieve the array can i use the same function and replace the put by get ? – user3368764 Mar 08 '14 at 17:05
  • is this still applicable isChkb.add(settings.getBoolean("Status_" + i, false)); after using the puBoolean() so i store boolean values – user3368764 Mar 08 '14 at 17:22
2

To store an array in sharedPreferences , you can put your array values in a String , then store this String .. and if you you want to get your array value (in our case ,stored in a string) , you get the string and parse it using StringTokenizer like this : (In my example I will store and retreive an array of Boolean)

  • Put your booleans into a string, delimiting every int by a character, for example a comma, and then save them as a string:

    SharedPreferences prefs = getPreferences(MODE_PRIVATE);
    Boolean[] list = new Boolean[10];
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < list.length; i++) {
    str.append(list[i]).append(",");
    }
    prefs.edit().putBoolean("keys", str.toString()).commit();
    
  • Get the string and parse it using StringTokenizer:

    String savedString = prefs.getString("string", "");
    StringTokenizer st = new StringTokenizer(savedString, ",");
    Boolean[] savedList = new Boolean[10];
     for (int i = 0; i < 10; i++) {
        savedList[i] = Boolean.valueOf(st.nextToken();
      }
    
Zied R.
  • 4,964
  • 2
  • 36
  • 67
  • you'r welcome , i use this way because it's simple and do what i want perfectly – Zied R. Mar 08 '14 at 17:33
  • i am having trouble understanding what happens at the first launch of the application where the preferences are not yet present doesn't it return an error ? – user3368764 Mar 08 '14 at 17:35
0

Use key names instead of isChk to get value of keys from SharedPreferences because isChk is SharedPreferences name :

keys[i] = settings.getBoolean("keys", false);

Also you are saving all values with same key keys so you only get last value which you have added. to store all values use unique keys like "keys"+i.

For more how we add Array in SharedPreferences see this post:

Save ArrayList to SharedPreferences

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213