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

Unable to remove object from string of objects saved in sharedPreferences

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

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

对象class:

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());
}

}

class 与 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;
}

}

为了可能遇到同样挑战的人,终于找到了解决问题的方法。我没有使用 removeFavorite() 方法,而是获取了 fPerson 的列表,删除了我想要的项目,然后在首选项中以相同的名称重新保存列表,如下所示... fp.remove(位置); myPref.saveFavorites(getApplicationContext(), fp);