Skip to content Skip to sidebar Skip to footer

Android- Adjacent Buttons In List View Automatically Clicked

I am working on a module that populates cart. I used ListView and extended BaseAdapter to populate the cart items. With each of the item in ListView, I embedded two button(inc and

Solution 1:

You are not selecting correct CartModel inside onClick, if you want to get correct object inside onclick then you have to tag position to button

holder.qntInc.setTag(position);

in onClick:

@Override
public void onClick(View view) {
    if (qntSpinnerCb != null) {
        CartModel cm= mCart.get((Integer)view.getTag);
        qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);
    }
}

Follow the same for qntDec.


Solution 2:

If you want to keep your current implementation, do this.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
    convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
}

ViewHolder holder = new ViewHolder();

holder.baseItem = (TextView) convertView.findViewById(R.id.qnt_tv);
holder.qntInc = (TextView) convertView.findViewById(R.id.inc_btn);
holder.qntDec = (TextView) convertView.findViewById(R.id.dec_btn);


...

Without seeing the rest of your code, however, I think you may want to reconsider how you are implementing BaseAdapter. Traditionally, they are used in conjunction with a list view to recycle views for efficiency. This means when a list scrolls a view out of frame, instead of redrawing the view - if(convertView == null) - it reuses it. What I think you are experiencing is that you are putting a click listener for two different objects on the same button.

What I would recommend doing is something more like this:

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

@Override
public CartModel getItem(int position) {
    // mCart sounds like a single item but is a list? I advise you to rename
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    // Implement with id. Copied the id call from your code..
    return getItem(position).getmIid();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
    }

    CartModel cm = getItem(position);

    TextView baseItem = (TextView) convertView.findViewById(R.id.qnt_tv);
    baseItem.setText(cm.getmTitle());

    // Organize like items together for readability
    TextView qntInc = (TextView) convertView.findViewById(R.id.inc_btn);
    qntInc.setTag(mCartKey, cm);
    qntInc.setOnClickListener(this);

    // readability
    TextView qntDec = (TextView) convertView.findViewById(R.id.dec_btn);
    qntDec.setTag(mCartKey, cm);
    qntDec.setOnClickListener(this);


    // Defined in convert view?
    qntSel.setText(String.valueOf(cm.getmQnt()));


    return convertView;
}

@Override
public void onClick(View v) {

    switch(v.getId()){
        case R.id.R.id.inc_btn:
            CartModel cm = (CartModel)v.getTag(mCartKey);
            // What is this?
            if (qntSpinnerCb != null)
                qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);

            break;


        case R.id.dec_btn:

            CartModel cm = (CartModel)v.getTag(mCartKey);
            if (qntSpinnerCb != null) {
                qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), DECREASE_QNT);
            }
            break;
    }

}

NOTE: BaseAdapter class must implement OnClickListener


Solution 3:

store the position of your view in your viewholder and use it every where in the setonclicklistner from viewHolder that position you will not see any absurd behaviour it will work as you want

static class ViewHolder {
    TextView baseItem, qntInc, qntDec;
    ....../* your code goes here */
    int position
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
        holder = new ViewHolder();
        holder.baseItem = (TextView) convertView.findViewById(R.id.qnt_tv);
        holder.qntInc = (TextView) convertView.findViewById(R.id.inc_btn);
        holder.qntDec = (TextView) convertView.findViewById(R.id.dec_btn);
        holder.position=position
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.position=position;
    }
    CartModel cm = mCart.get(holder.position);
    holder.baseItem.setText(cm.getmTitle());
    holder.qntSel.setText(String.valueOf(cm.getmQnt()));
    holder.qntInc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) { 

            CartModel cm = mCart.get(holder.position);
            qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);
        }  
    });

    holder.qntDec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            CartModel cm = mCart.get(holder.position); 
            qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), DECREASE_QNT);

        }
    });

    return convertView;
}

also check the value of INCREMENT_QNT and DECREACE_QNT


Solution 4:

Your getter method getmIid() in CartModel has problem i suspect. This method is returning wrong index and so your next/previous button is updating

I suggest to use position integer in your callback changeQuantityOfSelectedItemInCart() first parameter. Because the position will always come correctly with getView() callback

SOLUTION :

    holder.qntInc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (qntSpinnerCb != null)
            qntSpinnerCb.changeQuantityOfSelectedItemInCart(position+"", INCREASE_QNT);
        }
    });
    holder.qntDec.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (qntSpinnerCb != null)
            qntSpinnerCb.changeQuantityOfSelectedItemInCart(position+"", DECREASE_QNT);
        }
    });

The position is int type and your callback needs String so we should use position+""

Hope this will work for you


Solution 5:

There is an issue in the code.

if (convertView == null) {
    convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
    holder = new ViewHolder();
    holder.baseItem = (TextView) convertView.findViewById(R.id.qnt_tv);
    holder.qntInc = (TextView) convertView.findViewById(R.id.inc_btn);
    holder.qntDec = (TextView) convertView.findViewById(R.id.dec_btn);

    // This should be added in the code
    holder.qntSel = (TextView) convertView.findViewById(R.id.sec_txt);

    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

Post a Comment for "Android- Adjacent Buttons In List View Automatically Clicked"