Problem with CheckBox and SQLite in Android Studio

0

It turns out that I have this class in which I keep the information of, among other things, 4 Checkboxes in my SQLite Database, but it does not matter if the Checkboxes are selected or not, I always load "No" in my database. data.

To better understand what I mean, I share the class where I did the aforementioned:

public class Dia extends AppCompatActivity {

private String day, reserva;
EditText etComentario;
Button bnGuardarC, bnSalirC;
TextView tvNumDia;
CheckBox cbBaniar, cbPasear, cbCortar, cbJugar;
RadioGroup rgTurno;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dia);

    reserva = getIntent().getStringExtra("reserva");

    rgTurno = findViewById(R.id.rgTurno);
    bnGuardarC = findViewById(R.id.bnGuardarC);
    bnSalirC = findViewById(R.id.bnSalirC);
    tvNumDia = findViewById(R.id.tvNumeroDia);
    cbBaniar = findViewById(R.id.cbBaniar);
    cbPasear = findViewById(R.id.cbPasear);
    cbCortar = findViewById(R.id.cbCortar);
    cbJugar = findViewById(R.id.cbJugar);
    etComentario = findViewById(R.id.etComentario);

    day = getIntent().getStringExtra("numdia");

    final String baniar, pasear, cortar, jugar;

    if(cbBaniar.isChecked()){
        baniar="Si";
    }else{
        baniar="No";
    }

    if(cbPasear.isChecked()){
        pasear="Si";
    }else{
        pasear="No";
    }

    if(cbCortar.isChecked()){
        cortar="Si";
    }else{
        cortar="No";
    }

    if(cbJugar.isChecked()){
        jugar="Si";
    }else{
        jugar="No";
    }

    TextView tvDia = findViewById(R.id.tvNumeroDia);
    tvDia.setText(day);

    final BaseDeDatos tablaCuidados = new BaseDeDatos(getApplicationContext());

    bnGuardarC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tablaCuidados.insertarCuidado(null, "#1111", day, rgTurno.getCheckedRadioButtonId(), baniar, pasear,
                    cortar, jugar, etComentario.getText().toString());
            Toast.makeText(getApplicationContext(), "¡Cuidado guardado con éxito!", Toast.LENGTH_SHORT).show();
        }
    });


}

}

As you can see, I check the Checkboxes for an If, and if .isChecked == true, I should keep "Yes" in its variable, otherwise I should keep "No". The app runs rather well, only when I go to see what was saved in my database, the fields in the table that refer to the Checkbox are all loaded in "No", that is, if the .isChecked does not It was working. Maybe I'm not using it the right way, could you help me?

I leave a screenshot below with the results of my database and checkboxes fields marked in yellow:

Thank you!

    
asked by Rodrigo 19.11.2018 в 22:13
source

1 answer

-1

First remove the end of the Strings baniar, walk, cut, play, since when you call onCreate () the checkboxes are deselected, therefore all these variables take the value of no (... isChecked () dan false all) and to the final being, if you modify the checkbox it does not matter since the variable defined as final when it takes value, does not change its value throughout the execution. And second, you should check the states of the checkboxes when you click on the button, not when the activity is created, then those checklists should be inside the clickListener and before the part of the code where you save the database!

    
answered by 19.11.2018 / 22:27
source