-1

I have values of radio buttons stored in resume method, but when I try to register the values, I have NO VALUE IS DETECTED. The weird thing is that I see the radio button selected but the value is zero. So, when i go to database i have a=0 and b=0 which means the radio buttons are not clicked, when i click again on radio button and after that the button, i have the right values in database, what i am looking for is i don't have to click again on radio button.

    public class ActivityUn extends Activity {
        public void  ajouter(View v) {

      db.open();               
          db.insertMENAGE(a,b);                   
          db.close();
          Toast.makeText(getApplicationContext(), "Données Enregistrées",            Toast.LENGTH_SHORT).show();
              } 


   @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_un);       

         Button bton = (Button)findViewById(R.id.ajoutUn);
         bton.setOnClickListener(new View.OnClickListener() {  

          @Override
             public void onClick(View v) {

                   ajouter(v);                                            }
         });

public void onRadioButtonClicked(View view) {
    // Is the button now checked?

        boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.rm_13_1:
                    if (checked)
                          a=1;
                        break;
                case R.id.rm_13_2:
                    if (checked)
                        a=2;
                        break;

                case R.id.rm_14_1:
                    if (checked)
                        b=1;

                    break;
                case R.id.rm_14_2:
                    if (checked)
                        b=2;
                       break;
                case R.id.rm_14_3:
                    if (checked)
                        b=3;
                    findViewById
                    break;
                case R.id.rm_14_4:
                    if (checked)
                        b=4;
                          break;
  }
}




@Override
protected void onPause() {
    super.onPause();
    SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,
        MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs3.edit();
    editor.putBoolean("questionA", rm_13_1.isChecked());
    editor.putBoolean("questionB", rm_13_2.isChecked());
    editor.putBoolean("questionC", rm_14_1.isChecked());
    editor.putBoolean("questionD", rm_14_2.isChecked());
    editor.putBoolean("questionE", rm_14_3.isChecked());
    editor.commit();
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,
        MODE_PRIVATE);
    rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
    rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
    rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
    rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
    rm_14_3 = (RadioButton) findViewById(R.id.rm_14_3);
    Boolean rm_13_1A = false;
    Boolean rm_13_2A = false;
    Boolean rm_14_1A = false;
    Boolean rm_14_2A = false;
    Boolean rm_14_3A = false;
    rm_13_1A = prefs3.getBoolean("questionA", false);
    rm_13_2A = prefs3.getBoolean("questionB", false);
    rm_14_1A = prefs3.getBoolean("questionC", false);
    rm_14_2A = prefs3.getBoolean("questionD", false);
    rm_14_3A = prefs3.getBoolean("questionE", false);
    rm_13_1.setChecked(rm_13_1A);
    rm_13_2.setChecked(rm_13_2A);
    rm_14_1.setChecked(rm_14_1A);
    rm_14_2.setChecked(rm_14_2A);
    rm_14_3.setChecked(rm_14_3A);
}

Here the xml :

      <RadioGroup
    android:id="@+id/rm_13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_13_1"
        android:layout_width="wrap_content"
        android:layout_height="52dp"
        android:onClick="onRadioButtonClicked"
        android:text="OUI" />

    <RadioButton
        android:id="@+id/rm_13_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:onClick="onRadioButtonClicked"
        android:text="NON" />

</RadioGroup>

<TextView
    android:id="@+id/qm_14"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text="14. Statut de l’enquêté (e) :"
    />

<RadioGroup
android:id="@+id/rm_14"
android:layout_width="838dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="0.00"
android:background="#FFF8DC"
android:orientation="horizontal" >

<RadioButton
    android:id="@+id/rm_14_1"
    android:layout_width="wrap_content"
    android:layout_height="59dp"
    android:onClick="onRadioButtonClicked"
    android:text="Chef de ménage " />

<RadioButton
    android:id="@+id/rm_14_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onRadioButtonClicked"
    android:text="Epoux (se) " />

<RadioButton
    android:id="@+id/rm_14_3"
    android:layout_width="wrap_content"
   android:onClick="onRadioButtonClicked"
    android:layout_height="wrap_content"
    android:text="Fils ou fille aîné (e) " />

<RadioButton
    android:id="@+id/rm_14_4"
    android:layout_width="wrap_content"
    android:onClick="onRadioButtonClicked"
    android:layout_height="wrap_content"
    android:layout_weight="0.29"
    android:text="Autre " />

Utman Alami
  • 131
  • 1
  • 2
  • 12

2 Answers2

1

I see two mistake in your code.

first you have rm_14_4 in your XML but i never seen in your code that initialized.

second both of bellow code refer to one Button please fix that:

rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_2);

you used RadioGroup so worked with:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();

for more info see this and

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
1

Use RadioGroup.getCheckedRadioButtonId() and RadioGroup.check() instead of calling RadioButton.isChecked() and RadioButton.setChecked() on individual radio buttons. Use RadioGroup.setOnCheckedChangeListener() (through code, not the xml layout) to listen to changes in a RadioGroup.

GareginSargsyan
  • 1,877
  • 15
  • 23
  • thanks for idea, i will try it and tell you result, just to make it clear i should change that in Onresume and OnPause methodes, could you help me with an example of code :) – Utman Alami Dec 28 '13 at 12:23