简体   繁体   English

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

[英]get unchecked and checked values from a checkbox

I'm trying to print the contents of my checkbox list. 我正在尝试打印复选框列表的内容。 I'd like to display all the unchecked (false values) and checked (true values) in the order that it appears in the list view. 我想按在列表视图中出现的顺序显示所有未选中的(假值)和选中的(真值)。 So far I can only get the true values, how can I get the unchecked false values? 到目前为止,我只能获取真实值,如何获取未经检查的错误值?

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);
    }

}

From the documentation available on Android Developers, you might need to use a combination of the value along with the key in order to get the desired result. 从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");
    }
}

You can also take a look at what getCheckedItemPositions() does. 您还可以查看getCheckedItemPositions()的功能。

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

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