zerocool 18
zerocool 18

Reputation: 45

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

}

Upvotes: 1

Views: 322

Answers (1)

Ameya Pandilwar
Ameya Pandilwar

Reputation: 2778

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.

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.

Upvotes: 2

Related Questions