5

i am searching for hours to find out why saving boolean list in shared preferences is not working.

The methods are not saving or loading something... the loadmethod always returns the alternative values: so list_size as 0 and the booleans as false.

First of all i create the list in my MainActivity and call the save Method:

SaveLoadTraining sLoad = new SaveLoadTraining();
    ArrayList<Boolean> listBoolTrain = new ArrayList<Boolean>();
    listBoolTrain.add(true);
    listBoolTrain.add(true);
    sLoad.saveArray(listBoolTrain);

The subclass SaveLoadTraining looks like this:

    package de.sebspr.app08.halle;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import de.sebspr.app08.MainActivity;

public class SaveLoadTraining {

    private Context context;
    public static final String PREFS_NAME = "ListFile";
    private ArrayList<Boolean> list;

    public SaveLoadTraining(){
        this.context = MainActivity.getMContext();
    }

    public void saveArray(ArrayList<Boolean> list){

        this.list = list;

        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();

        int size = list.size();
        editor.putInt("list_size", size);

        for (int i = 0; i < size; i++) {
            editor.remove("list_"+i);
        }
        for (int i = 0; i < size; i++) {
            editor.putBoolean("list_"+i, list.get(i));
        }       
        editor.commit();
    }

    public ArrayList<Boolean> loadArray(){

        SharedPreferences file = context.getSharedPreferences(PREFS_NAME, 0);
        list = new ArrayList<Boolean>();
        int size = file.getInt("list_size", 0);

        for(int i = 0; i<size;i++){
            boolean bool = file.getBoolean("list_"+i, false);
            list.add(bool);
        }
        return list;
    }
}

I can not figure out what is going wrong...

Perhaps i handle the context on a wrong way? I just call this method to get the context of the MainActivity:

...
mContext = this;
...
    public static Context getMContext(){
        return mContext;
    }
Simon
  • 14,407
  • 8
  • 46
  • 61
Bolic
  • 705
  • 2
  • 12
  • 30
  • 2
    `I can not figure out what is going wrong...` - neither can we because you haven't told us ;) – Simon Jul 27 '13 at 08:37
  • ehm, hehe ;) The methods are not saving or loading something... the loadmethod always returns the alternative values: so list_size as 0 and the booleans as false – Bolic Jul 27 '13 at 08:41
  • I've just copied your code and it works for me. Please show where you are loading and saving it or how you are testing. – Ken Wolf Jul 27 '13 at 09:16
  • i just figured out the problem. The code above i posted is how you said correct... the mistak is somewhereelse... – Bolic Jul 27 '13 at 09:23

2 Answers2

3

Just make the following changes in your code, and it should work. Take SharedPrefrences mSharedPrefs as your class variable.

public class SaveLoadTraining 
{
private Context context;
public static final String PREFS_NAME = "ListFile";
private ArrayList<Boolean> list;   
private SharedPreferences mSharedPrefs;

public SaveLoadTraining(){
    this.context = getApplicationContext();
    mSharedPrefs = context.getSharedPreferences(PREFS_NAME, 0);
}

Make 2 different Methods for removing and adding values to Shared Preferences and do it in two commits instead of 1 Commit.

1st Method for Removing the List

public void removeArray(ArrayList<Boolean> list)
{
SharedPreferences.Editor editor = mSharedPrefs.edit();

int size = list.size();

    for (int i = 0; i < size; i++) {
        editor.remove("list_"+i);
    }
    editor.commit();
 }

2nd Method for Adding the List

public void addArray(ArrayList<Boolean> list)
{
SharedPreferences.Editor editor = mSharedPrefs.edit();

    int size = list.size();
    editor.putInt("list_size", size);

    for (int i = 0; i < size; i++) {
        editor.putBoolean("list_"+i, list.get(i));
    }       
    editor.commit();
 }

I hope this will work.

Maveňツ
  • 1
  • 12
  • 50
  • 89
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • I figured out the mistake. It was somewhereelse in my code. So the code ist working fine lik i posted. Neverless... thanks ;-) – Bolic Jul 27 '13 at 09:25
  • @SalmanAyub i want to save/store List in Shared preferences how can i do that ? i dnt want to save strings of list i want to store complete arrayList in SharedPreferences – Erum Feb 07 '14 at 12:55
  • Please check this link http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Salman Khakwani Feb 07 '14 at 16:01
  • bdw how to fetch values from another activity & let me check whether it will work or not :P – Maveňツ Jul 28 '15 at 12:43
1
public static boolean saveArrayList()
{

SharedPreferences sp = SharedPreferences.getDefaultSharedPreferences(this);
SharedPreferences.Editor mEdit1 = sp.edit();
mEdit1.putInt("Status_size", sKey.size()); /* sKey is an array List*/ 

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

    return mEdit1.commit();     
}

**Loading Array Data from Shared Preferences**

 public static void loadArrayList(Context mContext)
 {  
    Shared Preferences mSharedPreference1 = PreferenceManager.getDefaultSharedPreferences(mContext);
    sKey.clear(); //skey is arraylist
    int size = mSharedPreference1.getInt("Status_size", 0);  

    for(int i=0;i<size;i++) 
    {
      sKey.add(mSharedPreference1.getString("Status_" + i, null)); //skey is arraylist
    }
 }
Vaishali Sutariya
  • 5,093
  • 30
  • 32