检测单击自定义ListView布局中的复选框?

时间:2012-09-29 22:17:04

标签: java android listview checkbox onitemclicklistener

我知道有关于同一主题的大量其他线索,但它们似乎都不适用于我的场景,我无法使用我的列表视图来处理他们的代码。基本上,我使用SimpleCursorAdapter用数据库中的项填充列表视图。每个listview行使用自定义布局,该布局由复选框和一行简单文本组成。如何检测复选框上的点击?我知道我需要使用OnItemClickListener,但我不知道如何将其合并到我的代码中。这是我的代码:

remindersCursorAdapter = new SimpleCursorAdapter(this,
                         R.xml.view_reminders_item_layout,
                         remindersCursor, new String [] { RemindersDAO.NAME },
                         new int[] { R.id.view_reminders_item_text } );

viewRemindersListView.setAdapter(remindersCursorAdapter);

R.xml.view_reminders_item_layout是自定义列表视图布局文件。如何从此文件中捕获复选框并为其设置单击侦听器?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

如果要在单击项目时选中该复选框,可以通过在onItemClick方法中设置Checkbox的已检查状态来执行此操作。

  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

       // now get the checkbox view. Then set the checked status.
        CheckBox checkbox = (CheckBox) view.findViewById(R.id.check_box);
        checkBox.setChecked(!checkbox.isChecked());
  }

如果要检测“仅点击”复选框,请在xml中设置focusable true。      //在自定义列表视图项中。它需要当前的视图焦点。

      <Checkbox>
          android:focusable="true"
      </Checkbox>