Mayra是对的-这个问题与ListView重用视图的方式有关。并不是说有9个
CheckedTextView
对象,每个视图一个。相反,在所有行中都有一个被重用。因此,您不能依赖CheckedTextView对象来保存项目是否被选中的状态。您需要一些额外的数据结构来保存是否检查了给定的行,例如,
ArrayList<Boolean> checkedStates = new ArrayList<Boolean>();
ith
如果
伊思
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
boolean currentlyChecked = checkedStates.get(position);
checkedStates.set(position, !currentlyChecked);
// Refresh the list
}
});
然后在视图代码中:
public View getView(int pos, View inView, ViewGroup parent) {
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.image_list, null);
}
this.c.moveToPosition(pos);
final TextView bTitle = (TextView) v.findViewById(R.id.btitle);
String bookmark = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
byte[] favicon = this.c.getBlob(this.c.getColumnIndex(Browser.BookmarkColumns.FAVICON));
if (favicon != null) {
ImageView iv = (ImageView) v.findViewById(R.id.bimage);
iv.setImageBitmap(BitmapFactory.decodeByteArray(favicon, 0, favicon.length));
}
bTitle.setText(bookmark);
// Change the state of the checkbox to match that of the row's checked state.
// This check box item is reused for every row, so we need to reset its state each
// time the row is rendered.
CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);
markedItem.setChecked(checkedStates.get(pos));
return (v);
}