SOLVED BELOW
See below for solution
I have a ListView with a custom adapter which extends CursorAdapter. When the user clicks on an item in the ListView I want to open a new Activity(launched from the openDigitalBusinessCardActivity method), with the new Activity based upon the particular item in the ListView that was clicked. The new Activity needs the field merchantID from the database so I need to access this and then pass to the new Activity in my Intent.
However, I cannot get working how to access the correct data in my database based upon the feedback of the ListView click listener.
Eclipse gives me Bad request for field slot errors. My code for my listener is below:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Click registered!",
Toast.LENGTH_SHORT).show();
db = myDbHelper.getReadableDatabase();
//Cursor cur = (Cursor) adapter.getCursor();
//cur.moveToPosition(position);
Cursor cur = (Cursor) parent.getItemAtPosition(position);
//Cursor cur = (Cursor)((MyCustomCursorAdapter) list.getAdapter().getItem(position));
int merchantID = cur.getInt(cur.getColumnIndex("merchantID")); //Get the merchantID
db.close();
openDigitalBusinessCardActivity(merchantID);
}
});
My database creation code is this:
private static final String DATABASE_CREATE =
"create table " + TABLE_SCORECARD + "("
+ COLUMN_MERCHANTID + " integer primary key, "
+ COLUMN_MERCHANTNAME + " text not null, "
+ COLUMN_MERCHANTTAGLINE + " text not null, "
+ COLUMN_CURRENTSCORE + " Integer, "
+ COLUMN_FREEBIEPOINT + " Integer, "
+ COLUMN_CAMPAIGNID + " Integer, "
+ COLUMN_LISTPOSITION + " Integer);";
—UPDATE—
I have added an _id column to my table and now it looks like my query by the _id from onItemClick is not returning anything as my test on cur.moveToFirst() returns false. So onItemClick is not returning a valid table row number.
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Click registered!",
Toast.LENGTH_SHORT).show();
db = myDbHelper.getReadableDatabase();
//Cursor cur = (Cursor) adapter.getCursor();
//cur.moveToPosition((int) id);
//Cursor cur = (Cursor) parent.getItemAtPosition((int) id);
//Cursor cur = (Cursor)((MyCustomCursorAdapter) list.getAdapter().getItem(position));
Cursor cur = db.rawQuery("select merchantID from scoreCard where _id = "+id,null);
if(cur.moveToFirst()) {
//int merchantID = cur.getInt(cur.getColumnIndex("merchantID")); //Get the merchantID
Toast.makeText(getApplicationContext(), "cur.moveToFirst returned true",
Toast.LENGTH_SHORT).show();
}
cur.close();
db.close();
// openDigitalBusinessCardActivity(merchantID);
}
});
In the case of
Cursorbased adapters you get theidof the row in theonItemClickcallback, is theidparameter. You should use that.