【问题标题】:Removing element from arraylist inside recyclerview adapter, NotifyDatasetChanged inside the OnBindViewHolder Method从recyclerview适配器内的arraylist中删除元素,OnBindViewHolder方法内的NotifyDatasetChanged
【发布时间】:2019-04-02 14:01:45
【问题描述】:

我在我的 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

上面的代码适用于删除,但我不能在 bindview 方法中执行 notifyDatasetChanged() 在这种情况下有什么方法可以使用 notifydatasetChanged()

【问题讨论】:

  • 你能分享你的适配器整个代码吗
  • 我已经更新了代码请检查上面的代码
  • 使用listItems1.removeAt(position);而不是listItems1.remove(position);,然后通知你的recyclerviewlistItems1.add(position, newitem);而不是listItems1.set(position, newitem);
  • 但是我如何通知我的recyclerview?
  • 你要通知的适配器

标签: android android-adapter


【解决方案1】:

尝试在 UI 线程上运行它

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

【讨论】:

  • 你能解释一下我应该如何以及在哪里做吗?
  • 您是否尝试过通知数据,如果没有请在此行 listItems1.remove(position) 之后调用 notifyDataSetChanged()
  • 是的,我在 remove() 方法之后尝试过,但是当 RecyclerView 正在计算布局或滚动 androidx.recyclerview.widget.RecyclerView 时,它显示错误无法调用此方法
【解决方案2】:

您不在Adapter 的范围内,而是在您的OnStepCallback 范围内。要访问适配器的范围,您可以尝试:YourAdapter.this.notifyDataSetChanged();

【讨论】:

  • RecyclerView 正在计算布局或滚动时无法调用此方法
  • 适配器的onBindViewHolder()方法里面有没有其他调用notifyDataSetChanged的方法。
猜你喜欢
  • 2019-01-15
  • 2016-04-08
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 2020-05-06
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多