John Ranniel Sinel
John Ranniel Sinel

Reputation: 87

How can I get the selected value of the checked checkboxes written programmatically in android?

I have a CheckBox written programmatically in android, it is programmatically written because the value of the CheckBox will be based on online content, so basically, the program has no idea on how many CheckBox will going to print. I use for loops to print checkboxes, and it was successful, but among those checkboxes, I want to get the value of the CheckBox that will going to be selected.
How can I do that?

String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
    CheckBox cb = new CheckBox(survey);
    cb.setText(ansar[z]);
    choiceslayouts.addView(cb);
}

Upvotes: 0

Views: 749

Answers (2)

Linh
Linh

Reputation: 61019

You need to create a boolean array for saving the state of all checkbox
Then add setOnCheckedChangeListener for each CheckBox

String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
boolean[] array = new boolean[ansar.length];

for (int z = 0; z<ansar.length; z++){
       ...
       cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                array[z] = isCheck;
           }
       });
}

OR
You can set the tag for each CheckBox to check the state of CheckBox

1.Implement onClickListener for your Activity

public YourActivity extends Activity implements View.OnClickListener

2.Then set the tag for each CheckBox

for (int z = 0; z<ansar.length; z++){
    ...
    cb.setTag("tag"+z);   
    cb.setOnClickListener(this);
}

3.Finnaly, implement onClick

     public void onClick(View v) {
            switch (v.getTag()) {
                case "tag0":
                     if(((CheckBox) v).isChecked()) {
                          // your checkbox 0 is checked
                     }else{
                          // your checkbox 0 is not checked
                     }
                     break;
                case "tag1":
                     // do the same
                     break;
                ...
            }
        }

Hope this help

Upvotes: 1

Ruocco
Ruocco

Reputation: 573

I think you should use a ListView or a RecyclerView with custom layout and custom adapter, in order to optimize performance. Anyway it should be possible to achieve what you're look for without a major change to your code. Your problem is that in the for loop you recreate a new checkbox with the exact same properties as the previous one, so even if you add it to your parent view, you are basically adding the same checkbox over and over again, and you can't listen to the event in the "single" checkboxes. You should try something like this:

String ans = _jsonObject.optString("choices").toString();

String[] ansar = ans.split("`");
final boolean[] cbValue = new boolean[ansar.length];
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
    cb[z] = new CheckBox(survey);
    cb[z].setText(ansar[z]);
    cb[z].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cbValue[z] = !cbValue[z];
        }
    });
    choiceslayouts.addView(cb[z]);
}

cbValue[] should hold the values of your checkboxes.

This is without error-checking, but it should give you the idea.

Upvotes: 1

Related Questions