问题
I'm using a list view and there is a button called "Order" in the list item. I want to show a "tick" image when the user presses the button and hide it when the user presses the button again.
My issue is when I clicked on the button on the first item, tick image of the 4th and 8th item is also appeared.
This is the onClickListener in the adapter,
viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean isSelected = shoe.isSelected();
if(isSelected){
viewHolderItem.ivTick.setImageDrawable(null);
isSelected = false;
}else{
viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
isSelected = true;
}
shoeList.get(position).setSelected(isSelected);
obj.getAdapter().notifyDataSetChanged();
}
});
What is the reason for this issue?
update: This is my adapte getview method
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolderItem viewHolderItem;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.component_order_shoe_row,null);
viewHolderItem = new ViewHolderItem();
viewHolderItem.tvName = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_name);
viewHolderItem.tvPrice = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_price);
viewHolderItem.tvDesc = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_description);
viewHolderItem.ivPic = (ImageView)convertView.findViewById(R.id.iv_component_order_shoe_pic);
viewHolderItem.ivTick = (ImageView)convertView.findViewById(R.id.iv_component_order_shoe_tick);
viewHolderItem.spinnerSize = (Spinner)convertView.findViewById(R.id.spinner_component_order_shoe_size);
viewHolderItem.etQty = (EditText)convertView.findViewById(R.id.et_component_order_shoe_qty);
viewHolderItem.btnOrder = (Button)convertView.findViewById(R.id.btn_component_order_shoe_order);
convertView.setTag(viewHolderItem);
}else{
viewHolderItem = (ViewHolderItem)convertView.getTag();
}
final Shoe shoe = shoeList.get(position);
viewHolderItem.tvName.setText(shoe.getShoeName());
String text = String.valueOf(shoe.getPrice());
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;
if(decimalPlaces==1){
viewHolderItem.tvPrice.setText("Rs."+ text + "0");
}else{
viewHolderItem.tvPrice.setText("Rs."+ text);
}
viewHolderItem.tvDesc.setText(shoe.getDesc());
Bitmap bmp = AppControl.convertStringToBitmap(shoe.getImg());
viewHolderItem.ivPic.setImageBitmap(bmp);
int smallest = shoe.getSmallestSize();
int largest = shoe.getLargestSize();
Integer[] arr = getSizeArray(smallest,largest);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(mContext, android.R.layout.simple_spinner_dropdown_item,arr);
viewHolderItem.spinnerSize.setAdapter(adapter);
viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean isSelected = shoe.isSelected();
if(isSelected){
viewHolderItem.ivTick.setImageDrawable(null);
isSelected = false;
}else{
viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
isSelected = true;
}
shoeList.get(position).setSelected(isSelected);
obj.getAdapter().notifyDataSetChanged();
}
});
return convertView;
}
回答1:
You have to set the tag when the view is created for the first time so:
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//code
viewHolderItem.btnOrder= (ImageButton)rowView.findViewById(R.id.imageButton);
viewHolderItem.btnOrder.setTag(viewHolder); //set the tag for the button
convertView.setTag(viewHolder);
} else {
viewHolderItem = (ViewHolder) convertView.getTag();
}
And get it inside the click listener:
viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewHolderItem = (ViewHolder) v.getTag(); //get the tag inside on click
boolean isSelected = shoe.isSelected();
if(isSelected){
viewHolderItem.ivTick.setImageDrawable(null);
isSelected = false;
}else{
viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
isSelected = true;
}
shoeList.get(position).setSelected(isSelected);
obj.getAdapter().notifyDataSetChanged();
}
});
回答2:
You should add tag to every row :
in getView mehtod :
viewHolderItem.setTag(viewHolderItem);
and add getId() method in Show class and return id
change onclick listener like :
viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean isSelected = shoe.isSelected();
if(isSelected){
viewHolderItem.ivTick.setImageDrawable(null);
isSelected = false;
}else{
viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
isSelected = true;
}
showList(viewHolderItem.getTag()).setSelected(isSelected);
obj.getAdapter().notifyDataSetChanged();
}
});
The problem occurs becuse when you scroll up/down, getView() is called for every row i.e. for Row 4, 5 etc. So. the position
value changes very frequently. so we should use viewHolderItem.getTag()
value which will solve this problem
来源:https://stackoverflow.com/questions/29181500/listview-button-click-issue-in-android