如何监听列表视图行中的复选框?

发布于 2024-12-08 05:20:13 字数 1094 浏览 0 评论 0原文

String[] from = new String[] { CallrzDbAdapter.C_NAME,CallrzDbAdapter.C_EMAIL,CallrzDbAdapter.C_PHONE };
    int[] to = new int[] {R.id.cName, R.id.cEmail, R.id.cPhone };


    notes = new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);

    ListView view =getListView();
    view.setHeaderDividersEnabled(true);
    view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    view.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view,
                int position, long arg3) {
            Toast.makeText(getApplicationContext(), "Hello"+position+" is clicked ",
                      Toast.LENGTH_SHORT).show();

            return false;
        }
      });

    //setListAdapter(notes);
    setListAdapter(notes);

我有一个列表行的自定义布局,其中也有一个复选框。如何为复选框事件创建侦听器?我搜索并听说过bindView,但是有人可以更清楚地解释它吗? 是链接有人解释了它,但我无法将其插入我的代码中。

String[] from = new String[] { CallrzDbAdapter.C_NAME,CallrzDbAdapter.C_EMAIL,CallrzDbAdapter.C_PHONE };
    int[] to = new int[] {R.id.cName, R.id.cEmail, R.id.cPhone };


    notes = new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);

    ListView view =getListView();
    view.setHeaderDividersEnabled(true);
    view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    view.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view,
                int position, long arg3) {
            Toast.makeText(getApplicationContext(), "Hello"+position+" is clicked ",
                      Toast.LENGTH_SHORT).show();

            return false;
        }
      });

    //setListAdapter(notes);
    setListAdapter(notes);

I have a custom layout for the list row which has a checkbox as well. How can I create a listener for the checkbox event? I have searched and heard about bindView but is there anyone can explain it a bit more clearly? this is the link someone explained it but I couldnt plug it in my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落叶缤纷 2024-12-15 05:20:13

您可以创建自己的 ViewBinder 并在 setViewValue 中简单地执行以下操作:

 class MyViewBinder implements SimpleAdapter.ViewBinder {
  public boolean setViewValue(View view, Object data, String textRepresentation) {
   int id = view.getId();
   /* handle this particular item from our own view
   if (id == R.id.myViewId) {
    ((CheckBox) view).setOnItemLongClickListener(...);
    ((CheckBox) view).setText(...);
    return true;
   }
   return false;
  }
 }

然后您可以使用 SampleAdapter 获取数据并调用

adapter.setViewBinder(new MyViewBinder());

You can probably create your own ViewBinder and in setViewValue simply do something like:

 class MyViewBinder implements SimpleAdapter.ViewBinder {
  public boolean setViewValue(View view, Object data, String textRepresentation) {
   int id = view.getId();
   /* handle this particular item from our own view
   if (id == R.id.myViewId) {
    ((CheckBox) view).setOnItemLongClickListener(...);
    ((CheckBox) view).setText(...);
    return true;
   }
   return false;
  }
 }

You can thenjust use SampleAdapter for data and call

adapter.setViewBinder(new MyViewBinder());

べ映画 2024-12-15 05:20:13

onItemLongClick 中的 view 应包含您的复选框。

您可以像平常一样检索它:

Checkbox yourCheckbox = (Checkbox) view.findViewById(R.id.your_checkbox_id);

如果我错了,请纠正我,我通常使用自定义 ArrayAdapter

编辑:

您可以看一下这个示例。
Android 系列:自定义 ListView 项目和适配器
提示:在示例中的 getView 中,您可以findViewById 您的 CheckBox

The view from onItemLongClick should contain your checkbox.

You can retreive it like you normally would:

Checkbox yourCheckbox = (Checkbox) view.findViewById(R.id.your_checkbox_id);

Correct me if i'm wrong, I normally use a Custom ArrayAdapter

EDIT:

You could look at this for an example.
Android Series: Custom ListView items and adapters
Hint: it's the getView in the example where you can findViewById your CheckBox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文