Android- Adjacent Buttons In List View Automatically Clicked
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:
@OverridepublicvoidonClick(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.
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
}
ViewHolderholder=newViewHolder();
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:
@OverridepublicintgetCount() {
return objects.size();
}
@Overridepublic CartModel getItem(int position) {
// mCart sounds like a single item but is a list? I advise you to renamereturn objects.get(position);
}
@OverridepubliclonggetItemId(int position) {
// Implement with id. Copied the id call from your code..return getItem(position).getmIid();
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
}
CartModelcm= getItem(position);
TextViewbaseItem= (TextView) convertView.findViewById(R.id.qnt_tv);
baseItem.setText(cm.getmTitle());
// Organize like items together for readabilityTextViewqntInc= (TextView) convertView.findViewById(R.id.inc_btn);
qntInc.setTag(mCartKey, cm);
qntInc.setOnClickListener(this);
// readabilityTextViewqntDec= (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;
}
@OverridepublicvoidonClick(View v) {
switch(v.getId()){
case R.id.R.id.inc_btn:
CartModelcm= (CartModel)v.getTag(mCartKey);
// What is this?if (qntSpinnerCb != null)
qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);
break;
case R.id.dec_btn:
CartModelcm= (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
staticclassViewHolder {
TextView baseItem, qntInc, qntDec;
....../* your code goes here */int position
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
holder = newViewHolder();
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;
}
CartModelcm= mCart.get(holder.position);
holder.baseItem.setText(cm.getmTitle());
holder.qntSel.setText(String.valueOf(cm.getmQnt()));
holder.qntInc.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
CartModelcm= mCart.get(holder.position);
qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);
}
});
holder.qntDec.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
CartModelcm= 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(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
if (qntSpinnerCb != null)
qntSpinnerCb.changeQuantityOfSelectedItemInCart(position+"", INCREASE_QNT);
}
});
holder.qntDec.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(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"