Aminika
Aminika

Reputation: 143

I have trouble with radio button

I created 2 groups on radio-group one of them ask are you good in football? another one about basketball. and I gave score 3 if they are good and I want to calculate the total score in my program but I cant. I need help

private void addListenerOnButton() {
    final RadioGroup football = (RadioGroup) findViewById(R.id.radioGroup1);
    Button calc = (Button) findViewById(R.id.diabetriskbutton1);
    final RadioGroup basketball = (RadioGroup) findViewById(R.id.radioGroup2);
    calc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int IDfootball = football.getCheckedRadioButtonId();
            RadioButton football = (RadioButton) findViewById(IDfootball);
            if (IDfootball == R.id.item1yes) {
                int scorefootball = 3;

            } else if (IDfootball == R.id.item1no) { 
                int scorefootball = 2;

            } 

            int IDbasketball = basketball.getCheckedRadioButtonId();
            RadioButton basketball = (RadioButton) findViewById(IDbasketball);
            if (IDbasketball == R.id.item1yes) {
                int scorebasketball = 3;

            } else if (IDbasketball == R.id.item1no) { 
                int scorebasketball = 2;

            } 
            scoretotal = scorefootball + scorebasketball ;


        }
    });

}`  

Upvotes: 1

Views: 74

Answers (1)

Keale
Keale

Reputation: 4004

Try deleting these lines. I think your compiler confuses the RadioGroup football|basketball variable with the same local variables inside your onClick method.

I also think that they are unnecessary as they are never used after they are declared. (You are already comparing the ID obtained from getCheckedRadioButtonId() with R.id.itemYes|No

        RadioButton football = (RadioButton) findViewById(IDfootball);

        RadioButton basketball = (RadioButton) findViewById(IDbasketball);

Another point. You declared the scorefootball and scorebasketball inside the if and else blocks. The compiler will not see those variables outside of their respective blocks. This line will throw an error:

        scoretotal = scorefootball + scorebasketball;

Please declare them outside the like so:

        int scorefootball = 2;//default value
        int scorebasketball = 2;//default value

        ...

        if (IDfootball == R.id.item1yes) {
            scorefootball = 3;

        } else if (IDfootball == R.id.item1no) { 
            scorefootball = 2;

        } 

        ...

        if (IDbasketball == R.id.item1yes) {
            scorebasketball = 3;

        } else if (IDbasketball == R.id.item1no) { 
            scorebasketball = 2;

        }

        scoretotal = scorefootball + scorebasketball ;

Also can you please post your xml? I wonder why you are comparing the ID's obtained from the getCheckedRadioButtonId from different RadioGroups with the same R.id.item1Yes|No

Upvotes: 1

Related Questions