In my main activity mainActivity
I show a listview
of the data of my local database in SQlite
consulted through a class DataBaseHelper
, where the database was created and a class SQLcontrolle
where is the cursor method readData
which is finally the query itself, the query and the deployment of the same are made perfectly.
What I'm trying to do is that the moment they click on the ITEM, a message will be displayed with the ID
or the position or, failing that, the "ID" corresponding to the record in the list, this is what I have until now of code:
By clicking on ITEM, it launches me The application has been stopped of the selected record:
MainActivity.java (Updated 28-02)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
Firm = (RadioButton) findViewById(R.id.firma);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int itemPosition = position;
String itemValue = (String) lv.getItemAtPosition(position);
// Show Message
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
. The SQLController class where the Read Data cursor method is
SQLControler.java
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;
}
//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 finally DatabaseHelper where I create the database:
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);
}
Any help, suggestion and commentary are grateful in advance, thanks for your attention