我使用列表视图,并且有一个名为" Order"的按钮。在列表项中。我想要显示一个" tick"当用户按下按钮时隐藏图像并在用户再次按下按钮时隐藏它。
我的问题是,当我点击第一个项目上的按钮时,也会出现第4个和第8个项目的刻度图像。
这是适配器中的onClickListener,
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();
}
});
这个问题的原因是什么?
更新:这是我的自适应getview方法
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;
}
答案 0 :(得分:0)
首次创建视图时必须设置标记,以便:
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();
}
将其置于点击监听器中:
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();
}
});
答案 1 :(得分:-1)
您应该为每一行添加标记:
在getView mehtod中:
viewHolderItem.setTag(viewHolderItem);
并在Show类中添加getId()方法并返回id
更改onclick侦听器,如:
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();
}
});
当您向上/向下滚动时会出现问题,每行都会调用getView(),即行4,5等。 position
值经常变化。所以我们应该使用viewHolderItem.getTag()
值来解决这个问题