mercredi 2 mars 2016

Android Update Database with Checkbox

I've a class witch send a Query on my SQLite database. I want to update database when i check my checkbox, setting the value "1" when Checkbox is checked and "0" when the Checkbox is de-checked in the "visto" column.

I've never used SQL Query on Android so I've just follow some tutorials in internet to make this.

This is my class, i call it in my ActivityMain, onCreate:

SQL_visto

public class SQL_visto extends FrammentoUno {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startService(new Intent(SQL_visto.this, BackgroundService.class));
    new SQLMain(this);
}

public class SQLMain extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "web2park";

    static final String TABLE_NAME = "allarmi_ingressi";
    static final String COL_id_allarme = "id_allarme";
    static final String COL_id_park = "id_park";
    static final String COL_data_al = "data_al";
    static final String COL_stato = "stato";
    static final String COL_cod_allarme = "cod_allarme";
    static final String COL_descrizione = "descrizione";
    static final String COL_targa = "targa";
    static final String COL_visto = "visto";
    static final String COL_azione = "azione";
    static final String COL_varco = "varco";
    static final String COL_cassa = "cassa";

    private SQLiteDatabase database;

    public SQLMain (Context context) {
        super(context, DATABASE_NAME, null, 1);
        database = getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE allarmi_ingressi (id_allarme INTEGER PRIMARY KEY, " +
                "id_park TEXT, data_al TEXT, stato TEXT, cod_allarme TEXT, descrizione TEXT, " +
                "targa Text, visto TEXT, azione TEXT, varco TEXT, cassa TEXT );");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        android.util.Log.w(this.getClass().getName(),
                DATABASE_NAME + " database upgrade to version " + newVersion + " old data lost");
        db.execSQL("DROP TABLE IF EXISTS details");
        onCreate(db);
    }

    private void addListenerOnVisto(int id_allarme) {
        final CheckBox visto = new CheckBox(null);
        visto.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                if (((CheckBox) v).isChecked()) {
                    database.execSQL("UPDATE allarmi_ingressi SET visto = '1'");
                    visto.setChecked(true);
                    updateVistoColumn();
            }

            else if(((CheckBox)v).isChecked())
            {
                database.execSQL("UPDATE allarmi_ingressi SET visto = '0'");
                visto.setChecked(false);
                updateVistoColumn();
            }
        }
    });
    }
    public void updateVistoColumn()
    {
        database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        database.update(TABLE_NAME, values, COL_visto, null);
    }
}}




Aucun commentaire:

Enregistrer un commentaire