获取OnItemClick侦听器中listview项目内的复选框ID

时间:2016-10-27 23:05:03

标签: android listview checkbox

我有一个包含项目的列表视图。每个项目都有一个chekbox。现在,如果单击某个项目,我想更改此复选框的状态。如何在单击列表视图项时获取复选框的ID?

我收到了这段代码:

cList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) {
                Client currentClient = (Client) mAdapter.getItem(position);

            }
        });

3 个答案:

答案 0 :(得分:1)

在这里,我们循环遍历view对象中的所有视图并找到CheckBox

cList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        Client currentClient = (Client) mAdapter.getItem(position);

        try {
            ViewGroup group = (ViewGroup) view;
            if(group != null) {
                View currView;
                for(int i = 0; i < group.getChildCount(); i++) {
                    currView = group.getChildAt(i);
                    if(currView instanceof CheckBox) {
                        CheckBox checkBox = (CheckBox) currView;    // this is your checkbox
                        int checkboxId = checkBox.getId();
                        // do something
                        break;
                    } else {
                        // these are all the other views (not checkbox)
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }
});

答案 1 :(得分:0)

尝试:

var m = s.split(/\s+/).filter(function(i) { return !/\W/.test(i); });

&#34; Class&#34; - 声明mAdapter的类的名称。

答案 2 :(得分:-1)

将此添加到复选框: android:focusable="false" android:focusableInTouchMode="false"