【问题标题】:How to remove items from recyclerview adapter如何从 recyclerview 适配器中删除项目
【发布时间】:2020-07-27 06:39:00
【问题描述】:

如何从回收站视图适配器中删除项目。我从 api 获取项目并存储在 recyclerview 适配器中。我正在使用以下代码删除 mysql 数据库中的项目,但无法从 recyclerview 中删除项目

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.ordTxt.setText("Order No : "+mData.get(position).getOrderNumber());
        holder.ordCharges.setText("Rs: "+mData.get(position).getOrderCharges());

        orderNo = mData.get(position).getOrderNumber();


        holder.detBtn.setOnClickListener(v -> {
            Intent in_cat = new Intent(ctx,OrderDetail.class);
            in_cat.putExtra("user_id", preferenceHelper.getId());
            in_cat.putExtra("ordNo", mData.get(position).getOrderNumber());
            ctx.startActivity(in_cat);
        });

        holder.canBtn.setOnClickListener(v -> {
            cancelConfirmation();

        });


    }

call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    //Log.i("Responsestring", response.body().toString());
                    //Toast.makeText(ctx, "Response Got", Toast.LENGTH_SHORT).show();
                    if (response.isSuccessful()) {
                        if (response.body() != null) {

                            progressDialog.dismiss();

                            Log.i("onSuccess", response.body());

                            //deleteItem(mData.get(position));

                            String jsonresponse = response.body();
                            Toast.makeText(ctx,jsonresponse,Toast.LENGTH_LONG).show();

                            //success message for cancelling order to be set here

                            //JSONObject obj = new JSONObject(jsonresponse);
                            //if(obj.optString("status").equals("true")){


                            //parseLoginData(jsonresponse);

                        } else {

                            progressDialog.dismiss();

                            Log.i("onEmptyResponse", "Returned empty response");
                            //Toast.makeText(ctx,"Empty Response",Toast.LENGTH_LONG).show();
                        }
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    progressDialog.dismiss();
                    Toast.makeText(ctx,"Please connect your internet",Toast.LENGTH_LONG).show();

                }
            });

我正在使用改造来调用 api 并删除 mysql 中的项目,我如何获取选择项目的位置并将其从 api 成功消息的 recyclerview 中删除

【问题讨论】:

    标签: android arraylist android-recyclerview retrofit


    【解决方案1】:

    试试这个会起作用notifyItemRemoved(position)

    holder.canBtn.setOnClickListener(v -> {
        cancelConfirmation(position);
    });
    

    然后作为当前位置的参数传递如下cancelConfirmation(int position),最后在响应成功时删除项目如下

    if (response.isSuccessful()) {
        mData.remove(position)
        notifyItemRemoved(position);
    }
    

    看看official documentation

    【讨论】:

    • 但是我想在 api 响应后调用这个方法。 if (response.isSuccessful()) { if (response.body() != null) { 之后
    • 好的,cancelConfirmation是否包含api调用?
    • 是的,它包含在提到的代码中定义的 api 调用 call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { //Log.i("Responsestring", response.body().toString()); //Toast.makeText(ctx, "Response Got", Toast.LENGTH_SHORT).show(); if (response.isSuccessful() ) { if (response.body() != null) { progressDialog.dismiss();
    • 当我第一次删除项目时它是好的,但是当我删除最后一个项目时它会导致错误 java.lang.IndexOutOfBoundsException
    【解决方案2】:

    kotlin 版本:

    private fun deleteItem(position: Int) {
        mDataSet.removeAt(position)
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, mDataSet.size)
    }
    

    【讨论】:

    • api响应后如何调用该方法。 if (response.isSuccessful()) { if (response.body() != null) { 之后
    猜你喜欢
    • 2017-09-06
    • 2021-01-24
    • 2020-05-06
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    相关资源
    最近更新 更多