我创建了一个适配器来显示联系人。它有复选框,但如果我单击一个复选框,每个 8. 元素都会被触发,尽管 OnCheckedChanged 监听器只会触发一次。
我不知道我的适配器出了什么问题,也许问题是由于某些显示内容的方法引起的,我可能需要先覆盖这些方法。
可能是什么问题呢?
public class ContactAdapter extends SimpleCursorAdapter implements
OnCheckedChangeListener {
private Context context;
private int layout;
private Cursor cursor;
public Cursor getCursor() {
return cursor;
}
public ContactAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
cursor = c;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater vi;
View v = convertView;
if (v == null) {
v = ((LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layout, null);
ViewHolder holder = new ViewHolder();
holder.imageView = (ImageView) v.findViewById(R.id.contactImage);
holder.txtName = (TextView) v.findViewById(R.id.contactName);
holder.txtName.setTextSize(18);
holder.checkBox = (CheckBox) v.findViewById(R.id.checkBox);
v.setTag(holder);
}
ViewHolder holder = (ViewHolder) v.getTag();
Cursor cursor = (Cursor) getItem(position);
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
holder.txtName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
String photoID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
if (photoID != null) {
Uri photoUri = ContentUris.withAppendedId(
ContactsContract.Data.CONTENT_URI,
Long.parseLong(photoID));
holder.imageView.setImageURI(photoUri);
} else
holder.imageView.setImageResource(R.drawable.ic_launcher);
holder.checkBox.setTag(id);
holder.checkBox.setOnCheckedChangeListener(this);
return v;
}
@Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
//TODO
}
static class ViewHolder {
TextView txtName;
ImageView imageView;
CheckBox checkBox;
}
}