【发布时间】:2011-03-09 02:16:36
【问题描述】:
我一直在处理这个应用程序,但遇到了一个我无法解决的问题。我有一个列表视图,其中填充了来自适配器的内容,并且每一行都有其特定信息(统一)。当我尝试检索在该特定行中找到的复选框的值时,问题就出现了。
有问题的代码如下:
我构建了一个 AlertDialog 对象,以便我可以从用户那里获取我的信息。我的布局代码由水平方向的 LinearLayout 组成,其中包含图像、文本、复选框 3 个元素。我使用R.layout.listview_layout 构建我的AlertDialog,这是我制作的自定义布局。
不过,我尝试做的一件事是从适配器获取 CheckBox 视图;当我通过 cb.isChecked() 查看它时,无论我在哪一行,它总是未选中(又名错误)。为了进一步调试,我采用了相同的适配器并通过相同的方法检索文本,它返回了有关该行的特定信息,正如它应该返回的那样。
有什么办法可以解决这个问题吗?
简单地说:
我只想获取每个给定行的 CheckBox 的值
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();
}
感谢阅读!
【问题讨论】:
标签: android listview checkbox dialog