在RecyclerView中使用滑动删除视图时未在SharedPreference中删除

时间:2018-09-06 15:13:11

标签: android android-recyclerview sharedpreferences

我正在尝试删除RecylerView中的“通过滑动查看”,我在SharedPreferences中保存了已选中的“具有收藏夹”视图。在保存到SharedPreferences中的选定视图之后,我尝试用向左滑动向内移除收藏夹Activity,虽然这样做是可行的,但是当我返回收藏夹Activity时,我发现旧物件并没有当我向左滑动时更新了SharedPreferences

我该怎么做?

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<OrderModel> 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<OrderModel> loadFavorites(Context context) {
            SharedPreferences settings;
            List<OrderModel> favorites;

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

            return (ArrayList<OrderModel>) favorites;
        }


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

        public void removeFavorite(Context context, OrderModel beanSampleList) {
            ArrayList<OrderModel> favorites = loadFavorites(context);
            if (favorites != null) {
                favorites.remove(beanSampleList);
                storeFavorites(context, favorites);
            }
        }
    /*
        public void saveHighScoreList(String scoreString) {
            editor.putString(FAVORITES, scoreString);
            editor.commit();
        }

        public String getHighScoreList() {
            return settings.getString(FAVORITES, "");
        }
        */

    }




@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {

    if (viewHolder instanceof OrderAdapter.OrderViewHolder) {
        // get the removed item name to display it in snack bar
        String name = order_models.get(viewHolder.getAdapterPosition()).getOrder_name();


        final OrderModel deletedItem = order_models.get(viewHolder.getAdapterPosition());
        final int deletedIndex = viewHolder.getAdapterPosition();


        order_adapter.removeItem(viewHolder.getAdapterPosition());

        //remove from shared preferences
        sharedPreference.removeFavorite(Orders.this, deletedItem);
        order_models.remove(deletedItem);
        order_adapter.notifyDataSetChanged();

        Toast.makeText(Orders.this, "Success Remove",Toast.LENGTH_SHORT).show();

        // showing snack bar with Undo option
        Snackbar snackbar = Snackbar
                .make(constraint, name + " removed from cart!", Snackbar.LENGTH_LONG);
        snackbar.setAction("UNDO", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // undo is selected, restore the deleted item
                order_adapter.restoreItem(deletedItem, deletedIndex);
            }
        });
        snackbar.setActionTextColor(Color.YELLOW);
        snackbar.show();
    }

}

1 个答案:

答案 0 :(得分:1)

对于其中一个,您可能不应该每次要查询或修改列表时都从首选项加载收藏夹列表。而是,在创建此RecyclerView所属的活动时查询一次(您可以从适配器本身或从活动中执行此操作),并将其存储到全局变量中。即:

class SomeActivity extends Activity {
    private ArrayList<OrderModel> favorites = new ArrayList<>();
    private SharedPreference prefsHelper = new SharedPreference();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //....

        favorites.addAll(prefsHelper.loadFavorites(this));
    }
}

然后,当您要更改某些内容时,请修改该ArrayList,然后直接将其保存:

public void addFavorite(OrderModel model) {
    favorites.add(model);
    prefsHelper.storeFavorites(this, favorites);
}

您可能需要修改此代码以适合您的代码,但这只是操作示例。


您当前无法使用的内容,因为每次您修改列表时,都是从String表示形式重新创建它。这意味着您加载的收藏夹列表包含完全不同的模型实例,即使它们包含相同的值。

当您将OrderModel传递给removeFavorite()方法时,它不会删除任何内容,因为没有什么相等的。通过重新加载列表,您将拥有全新的实例。

如果您确实要保留当前的代码结构,请切换到索引而不是传递对象。或者,在OrderModel中覆盖equals()并手动比较值,以便甚至可以匹配不同的实例。