Foenix
Foenix

Reputation: 991

Get the position in a ListView for an row with a certain id

I have a ListView with a SimpleCursorAdapter and I wonder how to get a position in ListView for an id? I need it to perform a click on particular row of the list like this

lv.performItemClick(lv, pos, lv.getItemIdAtPosition(pos));

Upvotes: 0

Views: 1184

Answers (2)

user
user

Reputation: 87064

Iterate over your Cursor used in the SimpleCursorAdapter and check the ids:

if (cursor.moveToFirst()) {
    while (!cursor.isAfterLast) {
         if (cursor.getLong(/*the index of the _id column*/) == theTargetId) {
              theWantedPosition = cursor.getPosition();
              break; 
         }
         cursor.moveToNext();
    } 
}
lv.performItemClick(lv, theWantedPosition, lv.getItemIdAtPosition(theWantedPosition));

Upvotes: 6

Pradeep
Pradeep

Reputation: 2528

This override method will give you position of this clicked listView

 @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
    }

Upvotes: 0

Related Questions