Store a data type bool in an integer for DB

1

I need to store a boolean data in an integer, since I had seen that the SQLite android databases do not allow booleans. But I do not know how, it is to check if a RadioButton is marked, or vulgarly "checkeado". I tried this:

int r1 = radio1.isChecked() == true;

But it gives error. And then I need a if so that if said RadioButton is "checked", I inserted it in the table.

So I did it:

ContentValues registronuevo = new ContentValues();
            registronuevo.put("N", nombre);
            registronuevo.put("C", curso);
            if(r1 == true){
                registronuevo.put("R1", r1);
            }
            registronuevo.put("P4", cuatro);
            registronuevo.put("P5", cinco);

But the if also gives error.

EDITED:

Code to load the results:

public void cargar() {

    DB baseHelper = new DB(this, "DEMODB", null, 1);
    SQLiteDatabase db = baseHelper.getReadableDatabase();
    if (db != null) {
        Cursor c = db.rawQuery("select * from Datos", null);
        int cantidad = c.getCount();
        int i = 0;
        String[] arreglo = new String[cantidad];
        if(c.moveToFirst()){
            do{
                String linea = c.getInt(0)+"Nombre: "+ c.getString(1)+", Curso:"+c.getString(2)+", 1. R/:"+(c.getInt(c.getColumnIndex("R1"))>0?true:false)+ ", 4. R/:"+c.getString(4)+", 5. R/: "+c.getString(5);
                arreglo[i] = linea;
                i++;

            }while (c.moveToNext());
        }
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arreglo);
        ListView lista = (ListView) findViewById(R.id.lista);
        lista.setAdapter(adapter);
    }
}
    
asked by Pablo Gonzalez 09.03.2017 в 00:13
source

2 answers

0

Hello, nice to say hello, I think you should do this

ContentValues registronuevo = new ContentValues(); registronuevo.put("N", nombre); registronuevo.put("C", curso); if(r1 == true){ registronuevo.put("R1", 1); }else{ registronuevo.put("R1", 0); } registronuevo.put("P4", cuatro); registronuevo.put("P5", cinco);

And with that you can save the value in the database, when you read the record what you do is something like this assuming that you made the query to your database and all that

if(c.moveToFirst()){ (c.getInt(c.getColumnIndex("R1"))>0?true:false) }

    
answered by 09.03.2017 / 00:42
source
0

Hello, edit your code and you can leave it in the following way:

ContentValues registronuevo = new ContentValues();
registronuevo.put("N", nombre);
registronuevo.put("C", curso);
registronuevo.put("R1",r1==true?1:0);
registronuevo.put("P4", cuatro);
registronuevo.put("P5", cinco);

With this, if radio one is selected (true) the value of 1 will be entered in the New record, otherwise its value will be represented by a 0 (false).

    
answered by 09.03.2017 в 01:30