使用 notifyDataSetChanged() 后 ListView 未正确更新

ListView Not updating properly afte using notifyDataSetChanged()

在适配器 class 中,我正在尝试在点击事件后更新 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);
    }
}

您应该使用 notifyItemChanged(position) 而不是 notifyDataSetChanged(),这样您就不需要重新绑定所有视图。

但是如果您在 onBindViewHolder 中设置 OnClickListener,则不需要使用它。

onClick 方法中删除所有代码并放入此代码:

cartmodels.get(position).setFoodQty(cartmodels.get(position).getFoodQty()+1);
notifyDataSetChanged();