• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Where am I going wrong?

Greum

Well-Known Member
I am trying to get the value of a RadioGroup and have the following code, but I'm being told I can't set the value to true.

Java:
        final Boolean isFirstTimer = false;
        final Boolean isInvestor = false;

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(
            new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    switch(rb.getId()) {
                        case R.id.radioButton2:
                            isFirstTimer = true;
                            break;
                        case R.id.radioButton3:
                            isInvestor = true;
                            break;
                    }
                }
        });
 
You're confusing boolean and Boolean.
The former is a primitive type, whereas the later is a wrapper object.

For your purposes, declare your variables using the primitive type.
 
Hah! Thanks. I added final because without it I got an error

Variable isFirstTimer is accessed from within inner class, needs to be declared final

I forgot they can't be final as I need to change their value. I seem to be in some kind of Catch 22.
 
Define setter methods in the containing class, to set the values of these variables.
Then simply call these methods from the listener class when you need to change the values.
 
I'm afraid I'm in need of another pointer. I have got thus far and my brain hurts

Java:
public void setIsFirstTimer(boolean mIsFirstTimer) {
    isFirstTimer = mIsFirstTimer;
}
public void setIsInvestor(boolean mIsInvestor) {
    isInvestor = mIsInvestor;
}

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(
    new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb = (RadioButton) group.findViewById(checkedId);
            switch(rb.getId()) {
                case R.id.radioButton2:
                    //isFirstTimer = true;
                    this.setIsFirstTimer(true);
                    break;
                case R.id.radioButton3:
                    //isInvestor = true;
                    this.setIsInvestor(true);
                    break;
           }
       }
});
but there seems to be something wrong with my setter methods definitions...
 
Can we park this one for now. I know what your problem is, and can advise further, but I'm away from my computer and typing on a tablet device. It's a pain.
 
That's part of my problem. I can't think what object setIsFirstTimer() should operate on.

The examples I've worked with in tutorials create a new instance and setter methods are used on the new instance to assign them values.
 
Well the object 'instance' that you're using to invoke the setter methods is

new RadioGroup.OnCheckedChangeListener() {

And OnCheckedChangedListener has not defined those methods.
 
Hmm. So if I move the methods inside OnCheckedChangedListener am I getting warmer or colder?

Edit: I'm wondering if there is an easier way to get the value of a radiogroup. I can get the value of a checkbox simply with

Java:
        final CheckBox cbSisLHold = (CheckBox) findViewById(R.id.checkBox);
 
Last edited:
Hmm. So if I move the methods inside OnCheckedChangedListener am I getting warmer or colder?

Kind of :) It's possible to create your own version of OnCheckedChangedListener by extending that class.
You can then pass an object instance into the constructor method of your extended class.
By doing so,you can then invoke the methods defined on the class being passed in.

Sorry if this does not make sense but i really can not type code easily on this device.
 
Ah, okay. I understand that (surprisingly). Thanks for your help. It seems a lot of code just to get which radio button has been checked. I'm off to play again...

Sorry if this does not make sense but i really can not type code easily on this device.
I can relate to that!
 
Thing is, you are trying to set state variables on another class.
The recommended design pattern for doing that is to use setter methods.
I'm not saying this is the only way to do it, but it was the first thing that occurred to me.
I may have some other ideas when i can freely hack around some code on my regular computer.
 
Thing is, you are trying to set state variables on another class.
The recommended design pattern for doing that is to use setter methods.

Yes, I got that, thanks. But I couldn't for the life of me figure out how to get it to work. I can see that creating my own version of OnCheckedChangedListener as you suggested would work.

I've just gone back to the drawing board and come up with the following:

Java:
final RadioButton radioButton2 = findViewById(R.id.radioButton2);
final RadioButton radioButton3 = findViewById(R.id.radioButton3);
boolean isInvestor = radioButton2.isChecked();
boolean isFirstTimer = radioButton3.isChecked();

and it seems to do the job :D
 
Back
Top Bottom