Reputation: 1710
I have ListView
which have items from a database.
My adapter is this:
Cursor cur = myDb.getCategories();
// Closing the cursor
// DEPRECATED!!
startManagingCursor(cur);
// Set up mapping from cursor to view fields
String[] fromFieldNames = new String[] { DBAdapter.KEY_ITEM_CAT };
int[] toViewIDs = new int[] { R.id.tvItemCat };
// Create adapter to map columns of DB to elements on UI
myCursorAdapter = new SimpleCursorAdapter(this, // Context
R.layout.category_layout, // Raw layout template
cur, // Cursor (set of DB records)
fromFieldNames, // DB column names
toViewIDs // views ID to putt in list view
);
// Set the adapter for list view
LVCat.setAdapter(myCursorAdapter);
getCategories():
Cursor c = db.rawQuery("SELECT DISTINCT " + KEY_ITEM_CAT + " as " + KEY_ITEM_ID
+ ", " + KEY_ITEM_CAT + " FROM " + ITEMS_TABLE_NAME, null);
if (c != null) {
c.moveToFirst();
}
return c;
I want when i click on some item I should get item name. I've search and i found this code:
public void onCatClick(){
LVCat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
String category = LVCat.getItemAtPosition(position).toString();
// String category = parent.getItemAtPosition(position).toString(); --> This also I've tried, but the same result
System.out.println("Cat: " + category);
}
});
}
For other people this works. But I don't get the item name i get something like this:
System.out(23858): Cat: android.database.sqlite.SQLiteCursor@4197f3a0
So how can I get the selected item name?
Upvotes: 0
Views: 2540
Reputation: 384
Use parent.getItemAtPosition(position).toString()
in your onItemClickListener to get item.
or use it
TextView tv = (TextView) v.findViewById(R.id.tvItemCat);
String value = tv.getText().toString();
Upvotes: 1