在适配器类中,我尝试在点击事件后更新2 TextView
,
holder.increase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qty = Integer.parseInt(holder.itemQuantity.getText().toString());
qty += 1;
holder.itemQuantity.setText(String.valueOf(qty));
holder.totalItemPrice.setText(String.valueOf(getTotal(position))+" ৳");
updateCartItemQuantity(position,holder.itemQuantity);
}
});
如果我不使用notifyDataSetChanged()
,则首先TextView
更改,但不是第二次,但使用notifyDataSetChanged()
后也会停止。我在这里使用 BaseAdapter 。
适配器:
public class CartAdapter extends BaseAdapter {
private CartDatabaseSource cartDatabaseSource;
private Context context;
private ArrayList<CartModel> cartmodels;
private ShowCartItemsActivity showCartItemsActivity;
public CartAdapter(Context context, ArrayList<CartModel> cartmodels) {
cartDatabaseSource = new CartDatabaseSource(context);
this.context = context;
this.cartmodels = cartmodels;
this.showCartItemsActivity = (ShowCartItemsActivity) context;
}
private static class ViewHolder {
TextView itemName;
TextView itemPrice;
TextView itemQuantity;
TextView totalItemPrice;
TextView increase;
TextView decrease;
ImageView foodImage;
ImageView delete;
ImageView update;
}
@Override
public int getCount() {
return cartmodels.size();
}
@Override
public Object getItem(int position) {
return cartmodels.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.cart_single_row,parent,false);
holder=new ViewHolder();
holder.itemName = (TextView) convertView.findViewById(R.id.cartItemName);
holder.itemPrice = (TextView) convertView.findViewById(R.id.cartItemPriceValue);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.cartItemQuantityValue);
holder.totalItemPrice = (TextView) convertView.findViewById(R.id.cartItemTotalPriceValue);
holder.increase = (TextView) convertView.findViewById(R.id.increaseQuantity);
holder.decrease = (TextView) convertView.findViewById(R.id.decreaseQuantity);
holder.foodImage = (ImageView) convertView.findViewById(R.id.cartFoodImage);
holder.delete = (ImageView) convertView.findViewById(R.id.itemDeleteBtn);
holder.update = (ImageView) convertView.findViewById(R.id.updateQuantity);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context).load(cartmodels.get(position).getFoodImage()).into(holder.foodImage);
holder.itemName.setText(cartmodels.get(position).getFoodName());
holder.itemPrice.setText(String.valueOf(cartmodels.get(position).getFoodPrice())+" ৳");
holder.itemQuantity.setText(String.valueOf(cartmodels.get(position).getFoodQty()));
holder.totalItemPrice.setText(String.valueOf(getTotal(position))+" ৳");
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = cartmodels.get(position).getCartID();
final boolean status = cartDatabaseSource.deleteItem(id);
if(status){
context.startActivity(new Intent(context, ShowCartItemsActivity.class));
Toast.makeText(context, "deleted", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "couldn't delete", Toast.LENGTH_SHORT).show();
}
}
});
holder.update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = cartmodels.get(position).getCartID();
int qty = Integer.parseInt(holder.itemQuantity.getText().toString());
final boolean status = cartDatabaseSource.updateCartItem(id,qty);
if(status){
context.startActivity(new Intent(context, ShowCartItemsActivity.class));
Toast.makeText(context, "updated", Toast.LENGTH_SHORT).show();
holder.update.setVisibility(View.GONE);
}else{
Toast.makeText(context, "couldn't update", Toast.LENGTH_SHORT).show();
}
}
});
holder.increase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qty = Integer.parseInt(holder.itemQuantity.getText().toString());
qty += 1;
holder.totalItemPrice.setText(String.valueOf(getTotal(position))+" ৳");
holder.itemQuantity.setText(String.valueOf(qty));
updateCartItemQuantity(position,holder.itemQuantity);
}
});
holder.decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qty = Integer.parseInt(holder.itemQuantity.getText().toString());
if(qty > 1){
qty -= 1;
holder.itemQuantity.setText(String.valueOf(qty));
holder.totalItemPrice.setText(String.valueOf(getTotal(position))+" ৳");
updateCartItemQuantity(position,holder.itemQuantity);
}
}
});
return convertView;
}
private void updateCartItemQuantity(int position,TextView itemQuantity){
//notifyDataSetChanged();
int id = cartmodels.get(position).getCartID();
int qty = Integer.parseInt(itemQuantity.getText().toString());
final boolean status = cartDatabaseSource.updateCartItem(id,qty);
if(status){
if(context instanceof ShowCartItemsActivity){
//((ShowCartItemsActivity)context).setTotalPrice();
}
}else{
Toast.makeText(context, "couldn't update", Toast.LENGTH_SHORT).show();
}
}
private double getTotal(int position) {
int qty = cartmodels.get(position).getFoodQty();
double price = cartmodels.get(position).getFoodPrice();
return Math.round(price * qty);
}
}
答案 0 :(得分:1)
您应该使用notifyDataSetChanged()
而不是notifyItemChanged(position)
,而不需要重新绑定所有观看次数。
但如果您在OnClickListener
内设置onBindViewHolder
,则不应该使用此功能。
答案 1 :(得分:0)
从onClick
方法中删除所有代码并输入以下代码:
cartmodels.get(position).setFoodQty(cartmodels.get(position).getFoodQty()+1);
notifyDataSetChanged();