Adrian Olar
Adrian Olar

Reputation: 2903

Retrieve the listview position of a row knowing the object inside the row (MergeAdapter)

i am trying to scroll a list to a known object inside the row of a listview. Problem is that i don't know how to retrieve that position if i know the object that is contained inside.

Part of the problem is that i use a MergeAdapter (Commonsware) for a list with headers and subheaders.

Any hints on how i could achieve this?

THIS IS MY EDITED SOLUTION

folder_list.setAdapter(all_adapter);
        if (itemToBeSynced != null) {
            int skip = 0;

            for(int i=0;i<all_adapter.getCount();i++){
                if(all_adapter.getItem(i)!=null && all_adapter.getItem(i) instanceof MileageReceiptInterface){
                    if(all_adapter.getItem(i).equals(itemToBeSynced)){
                        skip = i;
                        break;
                    }
                }
            }

            final int finalSkip = skip;
            folder_list.post(new Runnable() {
                @Override
                public void run() {
                    folder_list.smoothScrollToPosition(finalSkip);
                }
            });
        }

Upvotes: 0

Views: 83

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

But i get a class cast exception for this line

You indicated that you have "headers and subheaders", which this line does not take into account. Use the instanceof operator to see if the returned adapter is a SyncItemAdapter, and proceed with your cast and the rest of this pass of the loop only if it is.

Or, skip the nested loop entirely. Call getCount() on all_adapter and iterate over it. For each pass, retrieve the Nth item via getItem(). If that is not null and is an instanceof MileageReceiptInterface, do your equals() check.

Upvotes: 1

Related Questions