Question

i have a problem in my application, i have a formulaire whish a user should fill information and save it to database,i have both Edit Text and Radio Button :

    rm_1 = (EditText) findViewById(R.id.rm_1);
    rm_2 = (EditText) findViewById(R.id.rm_2);
    rm_3 = (EditText) findViewById(R.id.rm_3);
    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);

i have a method Onclick whish associate each radio buton selected with a value :

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

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


    switch (view.getId()) {
    case R.id.rm_13_1:
        if (checked)
            a = 0;
            break;
    case R.id.rm_13_2:
        if (checked)
            a = 1;
        break;
    case R.id.rm_14_1:
        if (checked)
            b = 0;
        break;
    case R.id.rm_14_2:
        if (checked)
            b = 1;
        break;
    case R.id.rm_14_3:
        if (checked)
            b = 2;
        break;
    case R.id.rm_14_4:
        if (checked)
            b = 3;
        break;
    }

until now everything works fine, the user writes in the edit text and select the radio button , and in database i find the same information. in order to save the data entered by the user i did used shared preferences, so the text writing by the user and radio button selected appear again when the user returns to the activity.

That's when the issue occurs when a user change the activity , so if he returns to the activity he finds the radio button already selected but when he click on save button the value he gets in database is zero, it is like the methode :

                 public void onRadioButtonClicked(View view)

is not working, i don't know why ?? the user need to click again on radio button to have the values assigned in the method onRadioButtonClicked(View view), so how to solve that ?

this is where i save data :

           Button bton = (Button) findViewById(R.id.ajoutUn);
    bton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    ajouter(v);
        }
                  public void ajouter(View v) {
                  db.open();
                db.insertMENAGE(rm_1ts, rm_2ts, rm_3ts, rm_4ts, rm_5ts,
                rm_6ts, rm_7ts, rm_8ts, rm_9ts, rm_10ts, rm_11ts, 
                a, b, rm_14_4_autrets, rm_15ts);}}

And the method in database is :

                 public long insertMENAGE(String Region, String Provence_prefecture , String Commune_Arrondissement ,String N_district, String N_M_district , String N_menage_logement, String Adresse_menage , String Nom_Enqueteur, String code_enquêteur , String Date_realisation_enquête, String Nom_controleur , String Date_controle, int echantillon_principal, int Statut_enquêté , String autre, String Observations  ) {
      ContentValues initialValues = new ContentValues();
              initialValues.put(col_Commune_Arrondissement,Commune_Arrondissement);
      initialValues.put(col_N_district,N_district);
      initialValues.put(col_N_M_district,N_M_district);
      initialValues.put(col_N_menage_logement,N_menage_logement);
      initialValues.put(col_Adresse_menage,Adresse_menage);
      initialValues.put(col_Nom_Enqueteur,Nom_Enqueteur);
      initialValues.put(col_code_enquêteur ,code_enquêteur);
      initialValues.put(col_Date_realisation_enquête,Date_realisation_enquête);
      initialValues.put(col_Nom_controleur,Nom_controleur);
      initialValues.put(col_Date_controle,Date_controle);
      initialValues.put(col_echantillon_principal,echantillon_principal);
      initialValues.put(col_Statut_enquêté,Statut_enquêté);
      initialValues.put(col_Observations,Observations);

            return db.insertOrThrow(MENAGE,null, initialValues);
  }
Was it helpful?

Solution

Check all your radio button values at your onResume() method like following, and assigning values to your variable.

    if(rm_13_1.isChecked())
    {

    }
    else if(rm_13_2.isChecked())
    {

    }
    else if(rm_14_1.isChecked())
    {

    }
    else if(rm_14_2.isChecked())
    {

    }
    else if(rm_14_3.isChecked())
    {

    }

Hope it will work

OTHER TIPS

First of all I'm posting this an answer because the text is too large.

Either there's some meaningful code that we're not seeing, or there's a problem understanding a few things.

The method onRadioButtonClicked(View view) is only called when the user clicks the radioButton. When the user comes back from another Activity the button is already selected. This is correct, and this is how it should behave. As far as I can see, this code only affects variables 'a' and 'b'.

On the other hand you say that you have a separate 'save' button. When the user comes back from another Acivity, variables 'a' and 'b' should preserve their values, and the results of your 'save' code should work.

I know this is not an answer, just a few insights that hopefully help you in some way.

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