denispyr
denispyr

Reputation: 1443

Find listview position based on id

I have a ListView displaying data from a SimpleCursorAdapter. I have the id value and I want the position in the list, the opposite of getItemId(position). Is there such a functionality?

I know how to implement it (don't bother show code) but I just can't find it and it seems like an obvious method.

Upvotes: 2

Views: 1056

Answers (2)

Samuel Robert
Samuel Robert

Reputation: 11032

Simple solution would be to set the id to child item in bind view

@Override
public void bindView(View view, final Context context, Cursor cursor {
      ..... 
      convertView.setId(id);
}

Later from the activity you can find this view by id and get the position

View yourChildViewWithSpecifiedId = yourListView.findViewById(id);
int position = yourListView.getPositionForView(yourChildViewWithSpecifiedId);

Upvotes: 0

Sam
Sam

Reputation: 86948

Brought up from comments

There is no method like this for the built-in adapters. If your ListView is sorted by the row ids then you can use a binary search (fast!), otherwise you're probably stuck with an exhaustive search (slow...).

Upvotes: 1

Related Questions