我从firebase获取列表并在列表视图中显示它,当点击删除按钮时,我可以删除firebase中的项目,但问题是调用notifydatasetchanged()
,RecyclerView显示旧项目和新项目。
我需要知道如何防止RecyclerView显示旧项目以及我应该在哪里调用notifydatasetchanged()
public class CartRecyclerAdapter extends RecyclerView.Adapter<CartRecyclerAdapter.CartProductViewHolder> {
MyCartFragment myCartFragment = new MyCartFragment();
public List<CartProduct> cart_list = myCartFragment.cart_list;
public ArrayList<CartProduct> data = new ArrayList<>();
public CartRecyclerAdapter(List<CartProduct> cart_list) {
this.data.clear();
data.addAll(cart_list);
}
public class CartProductViewHolder extends RecyclerView.ViewHolder{
View mView;
TextView textView_name;
TextView textView_quantity;
TextView textView_price;
TextView order_quantity;
TextView order_price;
ImageView imageView;
ImageButton delete;
public CartProductViewHolder(View itemView) {
super(itemView);
mView = itemView;
textView_name = (TextView) itemView.findViewById(R.id.cproductname);
textView_price = (TextView) itemView.findViewById(R.id.cproductprice);
textView_quantity = (TextView) itemView.findViewById(R.id.cproductQuantity);
order_price = (TextView) itemView.findViewById(R.id.cuserprice);
order_quantity = (TextView) itemView.findViewById(R.id.cuserquantity);
imageView = (ImageView) itemView.findViewById(R.id.cproductimage);
delete = (ImageButton) itemView.findViewById(R.id.cdeletebutton);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int product_position = getAdapterPosition();
String pid = data.get(product_position).getId();
myCartFragment.removeitem(pid);
notifyDataSetChanged();
}
});
}
public void setName(String title) {
textView_name.setText(title);
}
public void setQunatity(Double quantity) {
textView_quantity.setText("Available Quantity: " + Double.toString(quantity));
}
public void setPrice(Double price) {
textView_price.setText("Price: " + Double.toString(price));
}
public void setOrderPrice(Double cart_price) {
order_price.setText("Price: " + Double.toString(cart_price));
}
public void setOrderQuantity(int cart_Quantity) {
order_quantity.setText(Integer.toString(cart_Quantity));
}
public void setImage(String image) {
Picasso.with(mView.getContext())
.load(image)
.placeholder(product)
.resize(1000,700)
.centerCrop()
.into(imageView);
}
}
@Override
public CartProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_cart_item, parent, false);
return new CartProductViewHolder(itemView);
}
@Override
public void onBindViewHolder(CartProductViewHolder holder, int position) {
CartProduct cartProduct = data.get(position);
holder.setName(cartProduct.getProductName());
holder.setQunatity(cartProduct.getQuantity());
holder.setPrice(cartProduct.getPrice());
holder.setImage(cartProduct.getUri());
holder.setOrderQuantity(cartProduct.getCart_quantity());
holder.setOrderPrice(cartProduct.getCart_price());
}
@Override
public int getItemCount() {
return data.size();
}
}
答案 0 :(得分:0)
您还必须从ArrayList中删除数据
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int product_position = getAdapterPosition();
String pid = data.get(product_position).getId();
myCartFragment.removeitem(pid);
data.remove(product_position);
notifyDataSetChanged();
}
});
答案 1 :(得分:0)
您可以使用适配器中的新数据创建一种删除旧数据的方法:
public class CartRecyclerAdapter extends RecyclerView.Adapter<CartRecyclerAdapter.CartProductViewHolder> {
...
public void swap(List<CartProduct> cartList) {
data.clear();
data.addAll(cartList);
notifyDataSetChanged();
}
}
然后,您可以调用它来更新列表:
yourAdapter.swap(cartList);
请检查以下代码是否有错误:
public class CartRecyclerAdapter extends RecyclerView.Adapter<CartRecyclerAdapter.CartProductViewHolder> {
MyCartFragment myCartFragment = new MyCartFragment();
public List<CartProduct> cart_list = myCartFragment.cart_list;
...
}
在myCartFragment
实例化的地方,然后您尝试获取CartProduct
的列表。由于创建片段的异步过程,myCartFragment尚未完成,因此会在代码中引入错误。