user1285334
user1285334

Reputation: 309

Android dynamic radiobuttons id

I am creating a number (count) of RadioButtons. I cant use RadioGroup, because i need 1 RadioButton, next to a Button, in each TableRow. Yet, as with all RadioButtons, only one should be picked at a time. I figured i could set the id, and read it on the onCheckedChanged, to change everything, but the one you clicked, to false.

rb = new RadioButton[count];

For-loop....
     rb[i]  = new RadioButton(this);
     rb[i].setId(5000 + i);
     rb[i].setOnCheckedChangeListener(this);

And the onCheckedChanged:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
{       
    for (int i = 0; i < count; i++)
    {
        if (buttonView.getId() == 5000 + i)
        {
            // Its the one
        }
        else
        {
            // Its not the one
            RadioButton rb = (RadioButton) findViewById((5000 + count));
            rb.setChecked(false);
        }
    }
}

I DO catch the right RadioButtons, but when i try the .setChecked(false) it gives me a NullPointerException, and i have no idea why.

Upvotes: 0

Views: 250

Answers (2)

user
user

Reputation: 87064

You're setting ids for your RadioButtons from 5000 to 5000 + (count - 1)(the RadioButton array has a size of count but the ids are until count - 1 because you start the loop from 0(?!?)). In the else clause you look for the RadioButton with the id 5000 + count which doesn't exist in the layout so you end up with a null reference.

Edit :

The code to simulate a RadioGroup should be like this:

In the for loop were you build the RadioButtons:

rb[i].setOnCheckedChangeListener(checkListener);
//...

where checkListener is a listener instance:

private OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            for (int i = 0; i < count; i++) {
                if (buttonView.getId() == 5000 + i) {
                    Log.e("XXX", "Position " + i);
                } else {                                       
                    RadioButton rb = (RadioButton) findViewById((5000 + i));
                    rb.setOnCheckedChangeListener(null);
                    rb.setChecked(false);
                    rb.setOnCheckedChangeListener(checkListener);
                }
            }
        }
    };

Upvotes: 1

Akshay
Akshay

Reputation: 2534

This is showing you NullPointerException because it is not getting id of checked RadioButton.Instead of this use isCheked() method to check weather radiobutton is checked or not.Try this one

  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
 {       
    for (int i = 0; i < count; i++)
  {
    if (buttonView.isChecked())
    {
        // perform your task here
    }
    else
    {
        // Do something here.........
    }
}

}

hope this helps :)

Upvotes: 0

Related Questions