首页 > 解决方案 > 从recyclerview适配器内的arraylist中删除元素,OnBindViewHolder方法内的NotifyDatasetChanged

问题描述

我在我的 recyclerview 布局中使用 Github 库 StepperTouch。在我的适配器内部,我试图在步进器为零时从数组列表中删除一个元素,或者在值增加或减少时更改数组列表中元素的值。问题是如何通知适配器我的bindviewholder()方法内部的变化是我的适配器

public class MyCartAdapter  extends RecyclerView.Adapter<MyCartAdapter.MyCartViewHolder>{
private List<AllItems> listItems1;
private Context context;

public MyCartAdapter(List<AllItems> listItems1, Context context) {
    this.listItems1 = listItems1;
    this.context = context;
}

@NonNull
@Override
public MyCartAdapter.MyCartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cart_items_layout, parent, false);
    return new MyCartViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull final MyCartAdapter.MyCartViewHolder holder, final int position) {


    final AllItems orderItem = listItems1.get(position);
    holder.setProductImage(orderItem.getImageUrl(),context);
    holder.name.setText(orderItem.getName());
    holder.setStepper(context);
    holder.stepperTouch.setCount(Integer.parseInt(orderItem.getQuantity()));
    String price = String.valueOf(orderItem.getPrice());
    holder.price.setText(price);
    holder.setComname(orderItem.getComname());

    final HashMap<String, AllItems> items = ((UserMainActivity)context).getItemMap();

    holder.stepperTouch.addStepCallback(new OnStepCallback() {
        @Override
        public void onStep(int value, boolean positive) {
            int count = holder.stepperTouch.getCount();
            if (count==0){
                AllItems item = items.get(orderItem.getName());
                if (item!=null){
                    String orderit = orderItem.getName();
                    ((UserMainActivity)context).removeItem(orderit);
                    listItems1.remove(position);
                }
            }
            else {
                    String quantity = String.valueOf(holder.stepperTouch.getCount());
                    String orderitemname = orderItem.getName();
                    String url = orderItem.getImageUrl();
                    String weight = orderItem.getWeight();
                    AllItems newitem = new AllItems(orderitemname ,orderItem.getComname(),url, quantity,weight,orderItem.getPrice());
                    ((UserMainActivity)context).addItem(orderitemname,newitem);
                    listItems1.set(position, newitem);
            }
        }
    });
}


@Override
public int getItemCount() {

    return listItems1.size();

}


public class MyCartViewHolder extends RecyclerView.ViewHolder {
    public TextView name,price,count,comname;
    public TextView weight;
    public ImageView productImage;

    public MyCartViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.ProName);
        price = (TextView) itemView.findViewById(R.id.proPrice);
        weight = (TextView) itemView.findViewById(R.id.ProWeight);
    }

    public void setProductImage(final String thumb_image, final Context ctx){

        productImage = (ImageView) itemView.findViewById(R.id.ProImage);
        Picasso.with(ctx).setIndicatorsEnabled(false);
        Picasso.with(ctx)
                .load(thumb_image)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .placeholder(R.drawable.basket_b).into(productImage, new Callback() {
            @Override
            public void onSuccess() {
            }
            @Override
            public void onError() {
                Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.basket).into(productImage);
            }
        });
}


    public void setComname(String name){
        comname = (TextView)itemView.findViewById(R.id.proComName);
        comname.setText(name);
    }
    public void setStepper(final Context context){
        stepperTouch = (StepperTouch) itemView.findViewById(R.id.stepper);
        stepperTouch.setMinValue(0);
        stepperTouch.setMaxValue(9);
        stepperTouch.setSideTapEnabled(true);
    }
    StepperTouch stepperTouch;
}

}

我试过使用MyCartAdapter.this.notifyDataSetChanged();,但它显示错误。RecyclerView 正在计算布局或滚动时无法调用此方法 androidx.recyclerview.widget.RecyclerView

上面的代码适用于删除,但我无法notifyDatasetChanged()在 bindview 方法内部进行,notifydatasetChanged()在这种情况下有什么方法可以使用。

标签: androidandroid-adapter

解决方案


尝试在 UI 线程上运行它

 runOnUiThread(new Runnable(){
  public void run() {
  // UI code goes here
 }
});

推荐阅读