Question

I was trying to make a rating type questions and i want to set the values in the radio button.. but i can't compute for the sum of the checked buttons.. this is my code..

public class MainActivity extends Activity {

public RadioButton r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
public RadioGroup rg1,rg2;
public TextView tv1;
public Button btn1;
public static int a,b,c,d,e,a1,b1,c1,d1,e1,total;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    r0 = (RadioButton)findViewById(R.id.radio0);
    r1 = (RadioButton)findViewById(R.id.radio1);
    r2 = (RadioButton)findViewById(R.id.radio2);
    r3 = (RadioButton)findViewById(R.id.radio3);
    r4 = (RadioButton)findViewById(R.id.radio4);
    r5 = (RadioButton)findViewById(R.id.radio5);
    r6 = (RadioButton)findViewById(R.id.radio6);
    r7 = (RadioButton)findViewById(R.id.radio7);
    r8 = (RadioButton)findViewById(R.id.radio8);
    r9 = (RadioButton)findViewById(R.id.radio9);


    btn1 = (Button)findViewById(R.id.button1);

    tv1 = (TextView)findViewById(R.id.textView7);

    rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
    rg2 = (RadioGroup)findViewById(R.id.radioGroup2);



    btn1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {


    switch(rg1.getCheckedRadioButtonId()){

    case R.id.radio0:
        if (r0.isChecked()){
            a =1;



        }
            break;
    case R.id.radio1:
        if (r1.isChecked()){
            b = 2;


        }
            break;
    case R.id.radio2:
        if (r2.isChecked()){
            c = 3;


        }
            break;
    case R.id.radio3:
        if (r3.isChecked()){
            d = 4;

        }
            break;
    case R.id.radio4:
        if (r4.isChecked()){
            e = 5;


        }
            break;
    }

    //no. 2
    switch(rg2.getCheckedRadioButtonId()){

    case R.id.radio5:
        if (r5.isChecked()){
            a1=1;

        }
            break;
    case R.id.radio6:
        if (r6.isChecked()){
            b1 = 2;


        }
            break;
    case R.id.radio7:
        if (r7.isChecked()){
            c1 = 3;

        }
            break;
    case R.id.radio8:
        if (r8.isChecked()){
            d1 = 4;

        }
            break;
    case R.id.radio9:
        if (r9.isChecked()){
            e1 = 5;

        }
            break;
    }

// i want to get the two checked radio button and output the result.. pls help me

    total = rg1.getCheckedRadioButtonId() + rg2.getCheckedRadioButtonId();

    tv1.setText("result: "+total);



}


    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

Was it helpful?

Solution

Look at this:

total = rg1.getCheckedRadioButtonId() + rg2.getCheckedRadioButtonId();

you are trying to add RadioButtonId's which are radio1, radio2, etc.

There are likely many ways to do this but I think I would create a sum variable that adds the chosen number to it.

case R.id.radio5:
    if (r5.isChecked()){
        //a1=1;
        sum += 1;
    }

and finally

tv1.setText("result: "+ sum);

you would need to reset sum each time so it doesn't keep adding to previous.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top