I’ve been searching on this for a while now, but I can’t find anything that could help me solve my problem.
I have a list of categories in a listview. I fetch these from a SQLiteDatabase and use a SimpleCursorAdapter to put them in the list.
This works fine…
Now, if I click a category, I want to launch a new activity displaying all items with that specific category. Passing parameters to the new activity isn’t a problem – found a nice tutorial on how to do this here: Passing data or parameter to another activity
My problem is that I can’t get the id out of the selected view… I want the id from the selected category so I can get the items with that categoryId.
This is where I try and get the id out of the view – I’ve used a lot of different methods (including some fiddling with listview, item and position, … this is my most recent attempt) and don’t know what to try next…
@Override
public void onItemClick(AdapterView<?> listview, View item, int position,
long itemId) {
Intent intent = new Intent();
Bundle bun = new Bundle();
bun.putInt("categoryId", (int) itemId);
intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
intent.putExtras(bun);
startActivity(intent);
}
Has anybody encountered the same problem and if you have, do you have some advice for me on how to do this?
This is how the problem got solved:
Cursor c = (Cursor)listview.getAdapter().getItem(position);
int id = c.getInt(c.getColumnIndex(_ID)); //0 = index id
//Log.d("Category id", id + "");
Intent intent = new Intent();
Bundle bun = new Bundle();
bun.putInt("categoryId", id);
intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
intent.putExtras(bun);
startActivity(intent);
Try following