リストアイテムをチェックし、間違ったチェックボックスのAndroidクリック
-
25-09-2019 - |
質問
私はSimpleCursorAdapterを拡張してカスタムリストビューを作成しました。 結果はIMAGE + CheckedTextView(テキスト+チェックボックス)です。
私は長い項目をクリックすると、すべてが正常に動作します - 私は、クリックされた項目の右のIDと詳細情報を取得
。私がチェックして項目をマークしようとしたが、それは間違っているチェックボックスをチェックする際に問題が発生します。
の例:の私は私のリスト上の9つのアイテムを持って、1-9をソート。私はlistItemの1をクリックした場合、ライン9上のチェックボックスがチェックされています。私はアイテム4をクリックした場合、6行目のチェックボックスがチェックされていると私は真ん中の行をクリックした場合、それがチェックされます。
は明らかに私はここで何かが欠けています:) 私は長い行をクリックすると、すべてが素晴らしい作品(のcontextMenuが開きます)覚えています。
のこれはリスナーです:の
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);
if (!markedItem.isChecked()) {
markedItem.setChecked(true);
} else {
markedItem.setChecked(false);
}
}
});
任意の助けに感謝!
あなたはより多くのコードを投稿するために私が必要な場合は、私に教えてくださいます。
ありがとうございます!
ところで、私は上のクリックした場合、複数の... PARTYが続くん...明らかな順序を...
EDIT:アダプタのコード
public class ImageCursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
private String url;
private TextView bUrl;
public ImageCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
}
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);
return (v);
}
}
解決
マイラは権利である - 問題は、リストビューは、あなたの意見を再利用されている方法に関係しています。 CheckedTextView
オブジェクトのインスタンス9、ビューごとに1があるかのようにそうではありません。代わりに、すべての行で再利用された単一のものがあります。したがって、あなたは項目がチェックされているかどうかの状態を保持するためにCheckedTextViewオブジェクトに依存することはできません。あなたは与えられた行は、例えばチェックされているかどうかを保持するためにいくつかの追加のデータ構造が必要になります、
ArrayList<Boolean> checkedStates = new ArrayList<Boolean>();
ith
行が確認されなければならないときに限りith
要素が真である。次に、あなたのitemClickListener内でます:
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);
}
これはあなたの問題を解決する必要があります。別のアプローチは、行が列が表すドメイン・オブジェクトにチェックインされているか否かのロジックを移動するであろう。それは私の好みになります。