繁体   English   中英

从复选框获取未经检查和检查的值

[英]get unchecked and checked values from a checkbox

我正在尝试打印复选框列表的内容。 我想按在列表视图中出现的顺序显示所有未选中的(假值)和选中的(真值)。 到目前为止,我只能获取真实值,如何获取未经检查的错误值?

public void selection (){
    final ListView lv = (ListView)findViewById(R.id.treeQuestionsList);
    Intent intent = getIntent();
     int id2 = intent.getIntExtra("id2", 0);
    SparseBooleanArray checked = lv.getCheckedItemPositions();
    int size = checked.size();

    for (int i = 0; i < size; i++) {
        int key = checked.keyAt(i);

    entries.add(new Observation(id2,lv.getCheckedItemPositions().get(key)));

        Log.d(Constants.TAG,"--ID:="+id2+"--checkeddata----"+ entries.get(i).answers);
    }

}

从Android Developers上可用的文档中,您可能需要结合使用值和密钥,以获得所需的结果。

for (int i=0; i < checked.size(); i++) {
    if (checked.valueAt(i)) {
        int key = checked.keyAt(i);
        Log.i(TAG, key + " is selected");
    } else {
        int key = checked.keyAt(i);
        Log.i(TAG, key + " is not selected");
    }
}

您还可以查看getCheckedItemPositions()的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM