无法从sharedPreferences中保存的对象字符串中删除对象

时间:2015-01-20 13:50:30

标签: android json sharedpreferences

我已经搜索了这个问题的解决方案,但我没有得到答案,但我不知道之前是否已经得到了回答。我在线获得了一个代码,使用Gson库将对象保存为SharedPreferences中的JSON字符串,并对其进行了调整以供使用。

除了我需要从SharedPreferences中删除项目时,代码工作正常。该项目不会删除。请帮我看看代码。代码粘贴如下所示。

对象类:

public class fPerson {

String name, id, type, cat;

public fPerson(String nm, String ID, String typ, String ct)
{
    this.name = nm;
    this.id = ID;
    this.type = typ;
    this.cat = ct;
}

public void setName(String n)
{
    this.name = n;
}

public void setId(String i)
{
    this.id = i;
}

public void settype(String ty)
{
    this.type = ty;
}

public void setCat(String ct)
{
    this.cat = ct;
}

public String getName()
{
    return this.name;
}

public String getId()
{
    return this.id;
}

public String getType()
{
    return this.type;
}

public String getCat()
{
    return this.cat;
}

public String toString()
{
    return new String(getName() + " " + getId()+ " " + getType());
}

}

具有SharedPreferences的类

public class Preferences {

public static final String PREFS_NAME = "APP";
public static final String FAVORITES = "Favorite";

public Preferences() {
    super();
}


public void saveFavorites(Context context, List<fPerson> favorites) {
    SharedPreferences settings;
    Editor editor;

    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();

    Gson gson = new Gson();
    String jsonFavorites = gson.toJson(favorites);

    editor.putString(FAVORITES, jsonFavorites);

    editor.commit();
}

public void addFavorite(Context context, fPerson person) {
    List<fPerson> favorites = getFavorites(context);
    if (favorites == null)
        favorites = new ArrayList<fPerson>();
    favorites.add(person);
    saveFavorites(context, favorites);
}

public void removeFavorite(Context context, fPerson person) {
    ArrayList<fPerson> favorites = getFavorites(context);
    if (favorites != null) {
        Iterator<fPerson> iter = favorites.listIterator();
        while(iter.hasNext())
        {
            fPerson temp = (fPerson)iter.next();
            if(temp.getId().equals(person.getId()))
            {
                favorites.remove(person);
                break;
            }
        }            
        saveFavorites(context, favorites);
    }
}

public ArrayList<fPerson> getFavorites(Context context) {
    SharedPreferences settings;
    List<fPerson> favorites;

    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

    if (settings.contains(FAVORITES)) {
        String jsonFavorites = settings.getString(FAVORITES, null);
        Gson gson = new Gson();
        fPerson[] favoriteItems = gson.fromJson(jsonFavorites, fPerson[].class);

        favorites = Arrays.asList(favoriteItems);
        favorites = new ArrayList<fPerson>(favorites);

    } else
        return null;

    return (ArrayList<fPerson>) favorites;
}

}

1 个答案:

答案 0 :(得分:0)

为了可能遇到同样挑战的人,终于找到了问题的解决方案。而不是使用removeFavorite()方法,我做的是获取fPerson列表,删除我想要的项目,并在相同的名称下重新保存列表,如此...     fp.remove(rposition);     myPref.saveFavorites(getApplicationContext(),fp);