我在recyclerview布局中使用Github库StepperTouch。在适配器内部,我尝试在步进器为零时从数组列表中删除一个元素,或者在值增大或减小时更改数组列表中元素的值。
问题在于如何在我的I-LOVE-STACK-OVER-FLOW|I-ALSO-LOVE-SO
方法内将更改的通知适配器
下面是我的适配器
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;
}
,但显示错误。
RecyclerView正在计算布局或滚动androidx.recyclerview.widget.RecyclerView时无法调用此方法
以上代码可用于删除,但是在这种情况下,bindview方法中的MyCartAdapter.this.notifyDataSetChanged();
无法执行。{p>
答案 0 :(得分:0)
您不在Adapter
的范围内,而是在OnStepCallback
的范围内。要访问适配器的范围,您可以尝试:YourAdapter.this.notifyDataSetChanged();
答案 1 :(得分:0)
尝试在UI线程上运行它
runOnUiThread(new Runnable(){
public void run() {
// UI code goes here
}
});