我正在寻找几个小时来找出为什么在共享首选项中保存布尔列表不起作用。
方法没有保存或加载某些东西...... loadmethod总是返回替代值:所以list_size为0,布尔值为false。
首先,我在MainActivity中创建列表并调用save方法:
SaveLoadTraining sLoad = new SaveLoadTraining();
ArrayList<Boolean> listBoolTrain = new ArrayList<Boolean>();
listBoolTrain.add(true);
listBoolTrain.add(true);
sLoad.saveArray(listBoolTrain);
子类SaveLoadTraining如下所示:
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;
}
}
我无法弄清楚出了什么问题......
也许我以错误的方式处理上下文? 我只是调用这个方法来获取MainActivity的上下文:
...
mContext = this;
...
public static Context getMContext(){
return mContext;
}
答案 0 :(得分:3)
只需在代码中进行以下更改即可。
以SharedPrefrences mSharedPrefs
作为您的类变量。
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);
}
制作2种不同的方法,用于删除和向共享首选项添加值,并在两次提交而不是一次提交中执行。
删除列表的第一种方法
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();
}
添加列表的第二种方法
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();
}
我希望这会奏效。
答案 1 :(得分: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
}
}