I know this might be dubbed as a duplicate question but i'm certain my case is quite unique. Basically i need a way to get my cursor id, that is the id from the database for my table parsed to the custom adapter. So that when i click on that list, it toast the correct id. Here is what i've got so far...
Please note that i want to use a customadapter and not a cursoradapter.
//Get Categories from database
final Cursor cursor = dbHandler.getCategories(0);
if (cursor != null) {
if(cursor.moveToFirst()){
do{
Categories categories = new Categories();
categories.set_id(cursor.getInt(0));
categories.set_categoryname(cursor.getString(2));
categories.set_categoriescaption(cursor.getString(3));
//list.add(cursor.getString(cursor.getColumnIndex(dbHandler.COLUMN_CATEGORY_NAME)));
categoriesList.add(categories);
}while (cursor.moveToNext());
}
cursor.close();
}
Cursor get the data from my sqlite database
listView = (ListView) view.findViewById(R.id.categories);
adapter = new CategoryAdapter(view.getContext(), R.layout.cursor_row, categoriesList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String cid = String.valueOf(id);
Toast.makeText(view.getContext(), cid , Toast.LENGTH_SHORT).show();
I want the real id of the table to be showed when i click the button and no the postion id. real id is supposed to b start from 1,, but i'm getting 0 here. Help will be appreciated. Thank you
After much pondering on the internet, i found a solution that now makes lot of sense.
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
The id from the onclick adapter view. I discovered that if it wasn't overridden, it will be set to the default position. So i passed my id in my customadapter and was able to see my id.
@Override
public long getItemId(int position) {
return data.get(position).Id;
}