You have to create a new RadioButton each time. Else, trying to re-use
the one radio button which is already added to the radio group, will
throw an error.
Setting the Radio Button before adding it, has this problem. If you
set the radio button after adding it to the radio group, it works
fine.
RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup01);
for (int i = 0; i < 6; i++) {
rg.addView(getRadioButton(i));
}
((RadioButton)rg.getChildAt(0)).setChecked(true);
private RadioButton getRadioButton(int index) {
RadioButton rb = new RadioButton(this);
rb.setText("Radio " + index);
return rb;
}
- Kumar Bibek
http://techdroid.kbeanie.com
On Sep 15, 7:37 pm, Christophe <[email protected]>
wrote:
> > The code you posted adds 10 RBs to a group. Then your get function returns a
> > new one that's not associated with anything.
>
> hu ? the only radio button I add are the one returned from the
> function :
>
> group.addView(getRadioButton(i));
>
> it is equivalent to the following code (which doesn't work either) :
>
> for (int i = 0; i < 10; i++) {
> RadioButton v = new RadioButton(this);
> if (i == 3) {
> v.setChecked(true);
> }
> group.addView(v);
>
> }
>
> On Sep 15, 3:42 pm, TreKing <[email protected]> wrote:
>
> > On Wed, Sep 15, 2010 at 8:35 AM, Christophe <
>
> > [email protected]> wrote:
> > > > 1 - Your getRadioButton function returns a new RadioButton.
>
> > > since I want 10 different radio buttons in my group, I can reuse the
> > > same radio button ?!?
>
> > The code you posted adds 10 RBs to a group. Then your get function returns a
> > new one that's not associated with anything. How do you expect that to work?
> > You're basically saying, "when getting a Radio Button, instead of getting
> > one I already created, make a new one and if was trying to get the first
> > one, set it to checked. Also, don't associate it with a group so its checked
> > state won't effect others."
>
> > > > 2 - You're using getApplicationContext().
>
> > > I have try with just "this" instead of "getApplicationContext()", but the
> > > problem is still there
>
> > This won't fix your problem (see #1), but you should avoid
> > getApplicationContext(). It will cause you problems elsewhere.
>
> > Should I use something else ?
>
> > Your most local Context ... usually "this".
>
> > On Wed, Sep 15, 2010 at 8:38 AM, Kumar Bibek <[email protected]> wrote:
> > > 1. New Radio button: I dont see a problem with that.
>
> > He already created 10 and the "new" ones don't get added to the group.
>
> > -------------------------------------------------------------------------------------------------
> > TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
> > transit tracking app for Android-powered devices
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en