保存ArrayList的共享preferences [英] Save ArrayList in shared preferences
问题描述
我寻找了几个小时,以找出原因节省布尔列表中共享preferences是行不通的。
该方法不保存或加载点什么... ... loadmethod总是返回的替代值:所以list_size为0,布尔为假
首先,我创建的列表中我MainActivity并调用保存方法:
SaveLoadTraining SLOAD =新SaveLoadTraining();
ArrayList的<布尔> listBoolTrain =新的ArrayList<布尔>();
listBoolTrain.add(真正的);
listBoolTrain.add(真正的);
sLoad.saveArray(listBoolTrain);
子类SaveLoadTraining看起来是这样的:
包de.sebspr.app08.halle;
进口的java.util.ArrayList;
进口android.content.Context;
进口android.content.Shared preferences;
进口de.sebspr.app08.MainActivity;
公共类SaveLoadTraining {
私人上下文的背景下;
公共静态最后弦乐preFS_NAME =的文件列表;
私人的ArrayList<布尔>清单;
公共SaveLoadTraining(){
this.context = MainActivity.getMContext();
}
公共无效saveArray(ArrayList中<布尔>列表){
this.list =清单;
共享preferences设置= context.getShared preferences(preFS_NAME,0);
共享preferences.Editor编辑器= settings.edit();
INT大小=则为list.size();
editor.putInt(list_size,大小);
的for(int i = 0; I<大小;我++){
editor.remove(名单_+ I);
}
的for(int i = 0; I<大小;我++){
editor.putBoolean(名单_+ I,list.get(I));
}
editor.commit();
}
公众的ArrayList<布尔> loadArray(){
共享preferences文件= context.getShared preferences(preFS_NAME,0);
名单=新的ArrayList<布尔>();
INT大小= file.getInt(list_size,0);
的for(int i = 0; I<大小;我++){
布尔布尔= file.getBoolean(名单_+ I,FALSE);
list.add(布尔);
}
返回列表;
}
}
我想不出什么错误...
也许我把手上的错误的方式的背景下? 我只是调用这个方法来获取MainActivity的背景下:
...
mContext =这一点;
...
公共静态上下文getMContext(){
返回mContext;
}
只要在你的code以下的变化,它应该工作。
以共享prefrences mShared preFS
作为类变量。
公共类SaveLoadTraining
{
私人上下文的背景下;
公共静态最后弦乐preFS_NAME =的文件列表;
私人的ArrayList<布尔>清单;
私人共享preferences mShared preFS;
公共SaveLoadTraining(){
this.context = getApplicationContext();
mShared preFS = context.getShared preferences(preFS_NAME,0);
}
请2不同的方法来删除和添加值共享preferences,做它在两次提交,而不是1提交。
对于删除列表第1种方法
公共无效removeArray(ArrayList中<布尔>名单)
{
共享preferences.Editor编辑= mShared prefs.edit();
INT大小=则为list.size();
的for(int i = 0; I<大小;我++){
editor.remove(名单_+ I);
}
editor.commit();
}
添加列表第2种方法
公共无效addArray(ArrayList中<布尔>名单)
{
共享preferences.Editor编辑= mShared prefs.edit();
INT大小=则为list.size();
editor.putInt(list_size,大小);
的for(int i = 0; I<大小;我++){
editor.putBoolean(名单_+ I,list.get(I));
}
editor.commit();
}
我希望这会工作。
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;
}
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.
这篇关于保存ArrayList的共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!