从android中的SharedPreferences中删除对象

问题描述 投票:0回答:1

我正在尝试当按收藏夹按钮将对象保存到SharedPreferences我已经完成了但是当我再次按下收藏夹按钮删除对象到SharedPreferences我不能这样做,我得到旧的对象没有删除对象我分享下面使用的代码为这个过程。我怎样才能做到这一点?

保存并删除到收藏夹对象

        String tag = holder.order.getTag().toString();
        if (tag.equalsIgnoreCase("deactive")) {
            //order_models.add(new RetroPhoto(1,dataList.get(position).getSurname(),dataList.get(position).getName()));
            sharedPreference.addFavorite(context, dataList.get(position));
            //dataList.get(position).getName();
            holder.order.setTag("active");
            holder.order.setImageResource(R.drawable.ic_favorite);
        } else {

            sharedPreference.removeFavorite(context, dataList.get(position));
            //dataList.get(position).getName();
            //dataList.remove(dataList.get(position));
            holder.order.setTag("deactive");
            holder.order.setImageResource(R.drawable.ic_favorite_outline);
        }



public class SharedPreference {

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

    public SharedPreference() {
        super();
    }


    public void storeFavorites(Context context, List<RetroPhoto> favorites){
        SharedPreferences settings;
        SharedPreferences.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 ArrayList<RetroPhoto> loadFavorites(Context context) {
        SharedPreferences settings;
        List<RetroPhoto> favorites;

        settings = 
        context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
        if (settings.contains(FAVORITES)) {
            String jsonFavorites = settings.getString(FAVORITES, null);
            Gson gson = new Gson();
            RetroPhoto[] favoriteItems = 
            gson.fromJson(jsonFavorites,RetroPhoto[].class);
            favorites = Arrays.asList(favoriteItems);
            favorites = new ArrayList<RetroPhoto>(favorites);
        } else
            return null;

        return (ArrayList<RetroPhoto>) favorites;
    }


    public void addFavorite(Context context, RetroPhoto beanSampleList) {
        List<RetroPhoto> favorites = loadFavorites(context);
        if (favorites == null){
            favorites = new ArrayList<RetroPhoto>();
        }
        favorites.add(beanSampleList);
        storeFavorites(context, favorites);
    }

    public void removeFavorite(Context context, RetroPhoto beanSampleList) {
        ArrayList<RetroPhoto> favorites = loadFavorites(context);
        if (favorites != null) {
            favorites.remove(beanSampleList);
            storeFavorites(context, favorites);
        }
    }
}

获取更新加载SharedPreferences

@Override
protected void onResume() {
    super.onResume();

    Log.e("onResume", "onResume Called");
    if(order_models != null ) {
        try {order_models = sharedPreference.loadFavorites(getApplicationContext());
            order_adapter = new OrderAdapter(getApplicationContext(), order_models);
            recycle.setAdapter(order_adapter);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
       order_adapter.notifyDataSetChanged();
    }
}
android sharedpreferences
1个回答
0
投票

实际上,您只需在SharedPreference中添加项目,并从阵列中删除项目。要从SharedPreference中删除项目,您应该使用此代码

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().remove("key").apply();
© www.soinside.com 2019 - 2024. All rights reserved.