我一直在研究這個應用程序,但遇到了一個我一直無法弄清楚的問題。我有一個用適配器內容填充的列表視圖,每行都有它們的特定信息(統一)。當我嘗試檢索在特定行中找到的複選框的值時,問題就出現了。AlertDialog.Builder訪問特定行的問題
有問題的代碼如下:
我建立一個AlertDialog對象,所以我可以從用戶那裏得到我的信息。我的佈局代碼由一個水平方向的LinearLayout組成,其中3個元素爲圖像,文本和複選框。我用R.layout.listview_layout
構建AlertDialog,這是我製作的自定義佈局。
我試圖做的一件事是從適配器獲取CheckBox視圖,但是;當我通過cb.isChecked()來查看它時,無論我在哪個行上總是未選中(又名爲false)。爲了進一步調試,我使用了相同的適配器,並通過相同的方法檢索了文本,並返回了有關該行的特定信息,因爲它應該如此。
任何想法如何處理?
簡單地說:
我只是想在每一個給定行獲得複選框的值
c = help.returnContacts();
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Select contacts");
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.listview_layout, c,
new String[] { ClientOpenDbHelperUtility.COL_NAME,
ClientOpenDbHelperUtility.COL_SEL }, new int[] {
R.id.txt_name, R.id.cb_select });
ab.setAdapter(adapter, null);
ab.setPositiveButton("Confirm", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Cursor c = adapter.getCursor();
for (int i = 0; i < adapter.getCount(); i++) {
CheckBox cb = (CheckBox) adapter.getView(i, null, null)
.findViewById(R.id.cb_select);
TextView t = (TextView) adapter.getView(i, null, null)
.findViewById(R.id.txt_name);
Log.d("DEBUG", "Checked = " + cb.isChecked());
Log.d("DEBUG", "Message = " + t.getText().toString());
if (cb.isChecked()) {
help
.updateSelection(
c
.getColumnIndex(ClientOpenDbHelperUtility.COL_UID),
true);
} else {
help
.updateSelection(
c
.getColumnIndex(ClientOpenDbHelperUtility.COL_UID),
false);
}
}
c.close();
help.closeAll();
}
});
ab.show();
}
感謝您的閱讀!
謝謝,我會試試! – JoxTraex 2011-03-09 05:08:57