【问题标题】:Unable to Refresh the RecyclerView after deleteing an Item删除项目后无法刷新 RecyclerView
【发布时间】:2021-09-30 06:39:28
【问题描述】:

我成功地从 recyclerView 中删除了一个项目。 Items被删除成功,但是recyclerView在删除后没有刷新。

这是我的Adapter 代码:

holder.removeProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences myPrefs = context.getSharedPreferences("loginToken", Context.MODE_PRIVATE);
            getToken = myPrefs.getString("token", null);

            new AlertDialog.Builder(context)
                    .setTitle("Remove from Cart")
                    .setMessage("Are you sure you want to remove this Item from cart ?")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            deleteDialog = ProgressDialog.show(context, "",
                                    "Please Wait...", true);

                            final RequestQueue requestQueue = Volley.newRequestQueue(context);
                            StringRequest request = new StringRequest(Request.Method.POST,
                                    REMOVE_CART_URL + "?token=" + getToken + "&product_id=" + cartModel.getProductid()
                                    , new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String message = jsonObject.getString("success");
                                        if (message.equals("true")) {

                                            Toast.makeText(context, "Item Deleted Successfully", Toast.LENGTH_SHORT).show();

                                        } else {
                                            Toast.makeText(context, "Some error occurred, Please try again", Toast.LENGTH_SHORT).show();

                                        }

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                        Log.e("Delete error", e.getLocalizedMessage());
                                    }

                                    deleteDialog.dismiss();

                                    notifyDataSetChanged();
                                }
                            },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {

                                            Toast.makeText(context, "Some error occurred, Please try again", Toast.LENGTH_SHORT).show();
                                            deleteDialog.dismiss();
                                        }
                                    });
                            requestQueue.add(request);


                        }
                    })

                    // A null listener allows the button to dismiss the dialog and take no further action.
                    .setNegativeButton(android.R.string.no, null)
                    .setIcon(android.R.drawable.ic_delete)
                    .show();

        }
    });

删除后的项目一直存在,直到我关闭应用程序并重新打开。我该如何解决这个问题,所以我不必每次都重新打开应用程序。

【问题讨论】:

  • 从外观上看,您似乎已从服务器中删除了该项目,但并未将其从支持此适配器的数据集中删除。
  • @IvanWooll 是的,就是这样。如何更新回收站视图
  • 试试notifyItemRemoved(position);
  • @Sniffer 在哪里。在哪一行,请解释一下
  • deleteDialog.dismiss();之前

标签: android android-recyclerview


【解决方案1】:

用下面的代码代替notifyDataSetChanged()

    if (yourAdapterList.size() > 0) {
        ourAdapterList.remove(position);
        notifyItemRemoved(position);
    }

【讨论】:

  • 成功了。但是,现在我收到了这个错误: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 ,我该如何解决这个问题
  • 您必须在通知之前检查列表的大小:if (yourAdapterList.size() &gt; 0) notifyItemRemoved(position); 我将编辑该代码。
  • 它仍然崩溃并显示此消息:java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 What do buddy
  • 运气不好,怎么办?
  • 你测试这个吗? if (yourAdapterList.size() &gt; 0) { ourAdapterList.remove(position); notifyItemRemoved(position); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多