我只想在listview的选定行显示图片。这是代码:
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public int getCount() {
return attendeeList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.attendee_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_dName = (TextView) convertView
.findViewById(R.id.txtDisplayName);
viewHolder.txt_dAddr = (TextView) convertView
.findViewById(R.id.txtDisplayAddr);
viewHolder.txt_dtelNo = (TextView) convertView
.findViewById(R.id.txtDisplayTelNo);
viewHolder.btn_scan = (ImageView) convertView.findViewById(R.id.btnScan);
viewHolder.registered_tag = (ImageView) convertView.findViewById(R.id.ivRegisteredTag);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_dName.setText(attendeeList.get(position)
.getAccountName().trim());
viewHolder.txt_dAddr.setText(attendeeList.get(position)
.getAttendeeAddr().trim());
viewHolder.txt_dtelNo.setText("Tel no: "
+ attendeeList.get(position).getTelNo().trim());
viewHolder.btn_scan.setOnClickListener(new OnClickListener(){
public void onClick(View v){
viewHolder.registered_tag.setVisibility(View.VISIBLE);
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
return convertView;
}
}
使用这些代码,我试图在btn_scan onClick上显示registered_tag。但是,图像并未真正显示在所选行中。它显示在另一行。
有什么想法吗?提前谢谢。
答案 0 :(得分:0)
当点击任何行时,您可能会获得最后一行图像更改,因为viewHolder
将包含调用getView
时的最后一行视图。使用setTag
视图的getTag
和viewHolder.btn_scan
方法在onClick方法中保存并获取选定的行视图:
viewHolder.btn_scan.setTag(viewHolder);
viewHolder.btn_scan.setOnClickListener(new OnClickListener(){
public void onClick(View v){
ViewHolder selectedViewHolder=(ViewHolder)v.getTag();
selectedViewHolder.registered_tag.setVisibility(View.VISIBLE);
...
}
});