Radio button in item and SQlite

0

I am trying to restate a previous question that is very confusing.

In the first place, taking into account that in the local database there is a series of data that have been previously inserted, I generate a query and I display the result of said query in a ListView. Each Item in the ListView contains a RadioButton that is unchecked by default.

The idea is that when you click on the radio control of the respective item, a data corresponding to the record of that item is updated.

I have the following classes:

DatabaseHelper. java

 public class DatabaseHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "GCM";
public static final String TABLE_NAME = "newsTable";
public static final String IDs = "_id";
public static final String MSG = "MESSAGE";
public static final String FIRMA = "firma";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + IDs + " INTEGER    PRIMARY KEY AUTOINCREMENT, " + MSG + " STRING, " + FIRMA + " STRING)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
} 

the class that controls the database

  public class SQLController {

private DatabaseHelper DatabaseHelper;
private Context ourcontext;
private SQLiteDatabase database;

public SQLController(Context c) {
    ourcontext = c;
}

public SQLController open() throws SQLException {
    DatabaseHelper = new DatabaseHelper(ourcontext);
    database = DatabaseHelper.getWritableDatabase();
    return this;

}

public void close() {
    DatabaseHelper.close();
}


//Getting Cursor to read data from table
public Cursor readData() {
    String[] allColumns = new String[] { DatabaseHelper.IDs, DatabaseHelper.MSG };
    Cursor c = database.query(DatabaseHelper.TABLE_NAME, allColumns, null,
            null, null, null, DatabaseHelper.IDs+" DESC",null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
 }


 } 

and inside the bundle in MainActivity the cursor that shows the query in the listview

     FIRMA = (RadioButton) findViewById(R.id.firma);
 //////TRASMO SQLITE QUE CONSULTA Y MUESTRA EN UN LIST VIEW /////
    dbcon = new SQLController(this);
    dbcon.open();
    lv = (ListView) findViewById(R.id.listView);

      Cursor cursor = dbcon.readData();
     String[] from = new String[] { DatabaseHelper.IDs,  DatabaseHelper.MSG};
     int[] to = new int[] { R.id.ids, R.id.msg};


     SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            MainActivity.this, R.layout.list_item, cursor, from, to);

    adapter.notifyDataSetChanged();
    lv.setAdapter(adapter);

Outside the bundle in the Main Activity class is the radiobutton method, but I do not know how to correctly call the method in the SQLcontroller class

   public void onRadioButtonClicked(View view) {
    // Is the button now checked?
      boolean checked = ((RadioButton) view).isChecked();
    String id = DatabaseHelper.IDs.toString();
    String firma = "ok";

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.firma:
            if (checked)



                dbcon.open();
            SQLController db = new SQLController(this);

            db.actualizar(String id, String firma);
            // notifies user

                Toast.makeText(getApplicationContext(), "FIRMA CORRECTS", Toast.LENGTH_LONG).show();
                // Pirates are the best
                break;

    } 
    
asked by JESUS ESPINOSA 23.02.2016 в 16:20
source

1 answer

1

First, to operate on the local database you need an object of type SQLiteDatabase . Then, to perform an update operation, you need to build a ContentValues object in which you enter the data you want to update and, finally, use the update method provided by the object of type SQLiteDatabase (db):

public int actualizar(int id, String firma){
  ContentValues contentValues = new ContentValues();
  contentValues.put(DatabaseHelper.FIRMA, firma);
  return db.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper.IDs + " = " + id, null);
}

So you just have to add the update method in your SQLController class.

  • Note: You must be careful not to name your variables exactly like your classes. This is wrong private DatabaseHelper DatabaseHelper; , this variable must be named otherwise, for example databaseHelper .

That said, the SQLController class would look like this:

public class SQLController {

    //Debes ir con cuidado de no nombrar la variable igual que la clase
    private DatabaseHelper databaseHelper; 
    private Context ourcontext;
    private SQLiteDatabase database;

    public SQLController(Context c) {
        ourcontext = c;
    }

    public SQLController open() throws SQLException {
        databaseHelper = new DatabaseHelper(ourcontext);
        database = databaseHelper.getWritableDatabase();
        return this;
    }

    public int actualizar(int id, String firma){
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.FIRMA, firma);
        return databaseHelper.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper.IDs + " = " + id, null);
    }

    public void close() {
        databaseHelper.close();
    }

    //Getting Cursor to read data from table
    public Cursor readData() {
        String[] allColumns = new String[] { DatabaseHelper.IDs, DatabaseHelper.MSG };
        Cursor c = database.query(DatabaseHelper.TABLE_NAME, allColumns, null, null, null, null, DatabaseHelper.IDs+" DESC",null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
} 
    
answered by 24.02.2016 / 10:54
source