简体   繁体   中英

Count textview changes while scrolling ListView

In my app,I am populating listview by the following method.

Saved all the datas (Product,Category,Count,Price) into SQLite DB.

Saved SQLite DB into List.

CartRes has getter & setter. Setting values and retrieve it from Adapter class by using getter.

Now I need to increase /decrease Count value by clicking increment / decrement button.

Values are changing while clicking.But while scroll listview all the textCount is set to default value of 1.

This is my Adapter Class:

public class CartCountBaseAdapter extends BaseAdapter {
    Context con;
    ArrayList<HashMap<String, String>> ArproductMap;
    String MYFRAGMENT;
    ViewHolder viewHolder;
    int pos;
    DbHelper dbHelper;
    DbUtil dbUtil;
    List<CartRes> restaurantCart_prducts;

    CartRes cartRes;

    public CartCountBaseAdapter(Context context, List<CartRes> cartBasket, String myfragment) {
        super();
        this.restaurantCart_prducts = cartBasket;
        this.con = context;
        this.MYFRAGMENT = myfragment;
    }

    @Override
    public int getCount() {
        return restaurantCart_prducts.size();
    }

    @Override
    public Object getItem(int position) {
        return restaurantCart_prducts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, final ViewGroup parent) {

        final ViewHolder viewHolder;
        LayoutInflater mInflater = (LayoutInflater) con.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        cartRes = restaurantCart_prducts.get(position);
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.cart_list_item, null);

            viewHolder = new ViewHolder();

            viewHolder.cartProduct = (TextView) convertView.findViewById(R.id.cartProduct);
            viewHolder.cartQuantity = (TextView) convertView.findViewById(R.id.cartQuantity);
            viewHolder.cartCount = (TextView) convertView.findViewById(R.id.cartCount);
            viewHolder.cartPrice = (TextView) convertView.findViewById(R.id.cartPrice);
            viewHolder.cartPriceDum = (TextView) convertView.findViewById(R.id.cartPriceDum);


            viewHolder.addTowish = (Button) convertView.findViewById(R.id.addTowish);
            viewHolder.remove = (Button) convertView.findViewById(R.id.remove);

            viewHolder.ivDecrease = (ImageView) convertView.findViewById(R.id.ivDecrease);
            viewHolder.ivIncrease = (ImageView) convertView.findViewById(R.id.ivIncrease);

            viewHolder.cardView = (CardView) convertView.findViewById(R.id.cardlist_item);

            if (MYFRAGMENT == "CheckOutFragment") {

                viewHolder.addTowish = (Button) convertView.findViewById(R.id.addTowish);
                viewHolder.remove = (Button) convertView.findViewById(R.id.remove);
                viewHolder.addTowish.setVisibility(View.GONE);
                viewHolder.remove.setVisibility(View.GONE);
                viewHolder.cardView.setCardElevation(0);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
                        (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                viewHolder.cardView.setLayoutParams(layoutParams);
            }

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {

            viewHolder.cartProduct.setText(cartRes.getCartproductName());
            viewHolder.cartQuantity.setText(cartRes.getCartcategoryName());
            viewHolder.cartCount.setText(cartRes.getCartCount() + "");
            viewHolder.cartPrice.setText(cartRes.getCartPrice() + "");
            viewHolder.cartPriceDum.setText(cartRes.getCartPrice() + "");


        } catch (Exception e) {
            e.printStackTrace();
        }

        viewHolder.ivIncrease.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                int Quantity = Integer.parseInt(viewHolder.cartCount.getText().toString());
                cartRes = new CartRes();
                cartRes.setCartCount(cartRes.increaseQuantity(Quantity));
                viewHolder.cartCount.setText(cartRes.getCartCount() + "");
            }
        });

        viewHolder.ivDecrease.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                int Quantity = Integer.parseInt(viewHolder.cartCount.getText().toString());
                Quantity--;
                Log.e("Base Position", String.valueOf(position));
                if (Quantity <= 0) {
                    Quantity = 0;
                }
                viewHolder.cartCount.setText("" + String.valueOf(Quantity));
            }
        });

        viewHolder.remove.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ArproductMap.remove(position);
                dbUtil = new DbUtil(con);
                dbUtil.open();
                dbHelper = new DbHelper(con);

                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    public static class ViewHolder {
        public TextView cartProduct,
                cartQuantity,
                cartCount,
                cartPrice,
                cartPriceDum;

        public int Quantity;
        public ImageView ivDecrease;
        public ImageView ivIncrease;
        public Button addTowish;
        public Button remove;
        public CardView cardView;
    }
}

Bean Class(Getter & Setter)

public class CartRes {
    private String CartcategoryName;
    private String CartproductName;
    private String product_id;
    private int CartPrice;
    private int CartCount;

    private int Quantity;

    public String getCartcategoryName() {
        return CartcategoryName;
    }

    public void setCartcategoryName(String cartcategoryName) {
        CartcategoryName = cartcategoryName;
    }

    public String getCartproductName() {
        return CartproductName;
    }

    public void setCartproductName(String cartproductName) {
        CartproductName = cartproductName;
    }

    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public int getCartPrice() {
        Log.e("Product Check BeanGet", String.valueOf(CartPrice));
        return CartPrice;
    }

    public void setCartPrice(int cartPrice) {
        CartPrice = cartPrice;
        Log.e("Product Check Bean", String.valueOf(CartPrice));
    }

    public int getCartCount() {
        Log.e("Product Check Count", String.valueOf(CartCount));
        return CartCount;
    }

    public void setCartCount(int cartCount) {
        Log.e("Product Check Count", String.valueOf(cartCount));
        CartCount = cartCount;
    }

    public int increaseQuantity(int quantity) {
        if (quantity < 1)
            quantity = 1;
        else
            quantity++;

        Quantity = quantity;
        return Quantity;
    }
}

Remove the methods, getViewTypeCount() and getItemViewType() . Move the replace cartRes = restaurantCart_prducts.get(position); with final CartRes cartRes = restaurantCart_prducts.get(position); inside the getView method.

Also remove the unwanted initialization of cartRes = new CartRes(); in ivIncrease onclicklistener.

Remove the global declaration of cartRes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM