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;
}