无法从保存在共享preferences对象的字符串中删除对象 [英] Unable to remove object from string of objects saved in sharedPreferences

查看:128
本文介绍了无法从保存在共享preferences对象的字符串中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找解决这个问题,但我没有得到一个答案,我不知道这是否已经然而面前回答。我得到了code在线保存对象在使用GSON库共享preferences JSON字符串并适应它使用了。

在code工作,除了罚款,当我需要从共享preferences删除项目。该项目不会删除。请帮我看看到code。 C $ CS中的$粘贴如下图所示。

对象类:

 公共类fPerson {字符串名称,编号,类型,猫;公共fPerson(字符串纳米,字符串ID,典型的字符串,字符串CT)
{
    this.name =纳米;
    this.id = ID;
    this.type =(典型值);
    this.cat =克拉;
}公共无效setname可以(字符串n)
{
    this.name = N;
}公共无效SETID(字符串一)
{
    this.id = I;
}公共无效的setType(字符串TY)
{
    this.type = TY;
}公共无效setCat(字符串克拉)
{
    this.cat =克拉;
}公共字符串的getName()
{
    返回this.name;
}公共字符串的getId()
{
    返回this.id;
}公共字符串的getType()
{
    返回this.type;
}公共字符串getCat()
{
    返回this.cat;
}公共字符串的toString()
{
    返回新的String(的getName()++的getId()++的getType());
}}

与共享preferences类

 公共类$ P $ {pferences公共静态最后弦乐preFS_NAME =APP;
公共静态最后弦乐收藏夹=收藏夹;公共preferences(){
    超();
}
公共无效saveFavorites(上下文的背景下,列表与LT; fPerson>的最爱){
    共享preferences设置;
    编辑器编辑;    设置= context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE);
    编辑= settings.edit();    GSON GSON =新GSON();
    字符串jsonFavorites = gson.toJson(收藏夹);    editor.putString(收藏,jsonFavorites);    editor.commit();
}公共无效加入收藏(上下文的背景下,fPerson人){
    清单< fPerson>收藏= getFavorites(背景);
    如果(收藏== NULL)
        最爱=新的ArrayList< fPerson>();
    favorites.add(人);
    saveFavorites(背景下,收藏);
}公共无效removeFavorite(上下文的背景下,fPerson人){
    ArrayList的< fPerson>收藏= getFavorites(背景);
    如果(收藏夹!= NULL){
        迭代器< fPerson> ITER = favorites.listIterator();
        而(iter.hasNext())
        {
            fPerson TEMP =(fPerson)iter.next();
            如果(temp.getId()。等于(person.getId()))
            {
                favorites.remove(人);
                打破;
            }
        }
        saveFavorites(背景下,收藏);
    }
}公众的ArrayList< fPerson> getFavorites(上下文的背景下){
    共享preferences设置;
    清单< fPerson>收藏夹;    设置= context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE);    如果(settings.contains(收藏夹)){
        字符串jsonFavorites = settings.getString(收藏,NULL);
        GSON GSON =新GSON();
        fPerson [] = favoriteItems gson.fromJson(jsonFavorites,fPerson []类);        收藏= Arrays.asList(favoriteItems);
        最爱=新的ArrayList< fPerson>(收藏夹);    }其他
        返回null;    回报(ArrayList的< fPerson>)收藏夹;
}}


解决方案

有关谁就可能具有相同挑战的目的,终于得到了一个解决问题的办法。除了使用removeFavorite()方法中,我所做的就是让fPerson列表,删除我要的项目,并在preferences重新保存列表下的同名像这样...
    fp.remove(rposition);
    我的pref.saveFavorites(getApplicationContext(),FP);

I have searched for solution to this problem but I have not got an answer, I don't know if it has been answered before however. I got a code online to save objects as JSON string in SharedPreferences using the Gson library and adapted it for use.

The code works fine except for when I need to remove an item from SharedPreferences. The item does not remove. Please help me look into the code. The codes are pasted as shown below.

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

}

The class with the 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;
}

}

解决方案

For the purpose of whoever might be having the same challenge, finally got a solution to the problem. Instead of using the removeFavorite() method, what I did was to get the list of fPerson, remove the item I want to and save the list afresh in preferences under the same name like so... fp.remove(rposition); myPref.saveFavorites(getApplicationContext(), fp);

这篇关于无法从保存在共享preferences对象的字符串中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆