marimaf
marimaf

Reputation: 5410

Filtering list view and getting correct onclick item

I have a list view and I've implemente filtering.

Lets say I have items A, B and C. If I type B in the filter box, only item B will be displayed and it is the position 0 of the list (before it was in position 1). So when I call the onClick item, I get the the id/position 0, which leads to displaying details about A instead of B.

This is the onclick code:

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

    Poi poi = pois.get((int)id);
    goPOIDETAIL(poi);

}
});

id and position have the same value.

is there a way to get the original position, or get some other value indicating the real item that I clicked?

Thanks

Upvotes: 6

Views: 9161

Answers (5)

FrancescoAzzola
FrancescoAzzola

Reputation: 2654

I think the problem is in the way you manage your filter. You should get the object with selected id not from the original List (or array) but from the filtered one.

I used something like it in this post from my blog. Hope this help you

Upvotes: 0

adev
adev

Reputation: 377

if you are using datbase you have the _id key that you can load in a filtered list as invisible field. Once you click on the item you can query data with _id key. If you aren't using a database you could add a hidden id element in your row element as well.

Upvotes: 0

raja
raja

Reputation: 2413

 flashsearchList.setOnItemClickListener(new OnItemClickListener() {

        @Override 
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Integer temp=flashSearchNameMap.get(adapter.getItem(position));

            navigateSearch(temp); 



        }
    }); 

(adapter.getItem(position) will return you the exact list name and in flashSearchNameMap i have stored names and position at beginning from oncreate before applying filtering.So you can get exact position by this

Upvotes: 4

Taras
Taras

Reputation: 2576

You can try:

@Override
public boolean hasStableIds() {
    return false;
}

in your adapter

Upvotes: 0

Andrii Chernenko
Andrii Chernenko

Reputation: 10194

ID and Index are not the same. Of course, you can return item index in getItemId() method of your adapter, but don't expect your items to be identified correctly by this method if you do. Try providing unique ID for each of your items. The idea is somewhat similar to ID of each record in the database, which never changes (and lets you reliably identify each record), and it is easily implemented when you get your data from database.

But if your items don't have unique IDs, and you don't want to bother providing them, there's another approach (see this example code for Adapter below):

public MyAdapter extends BaseAdapter {
    private List<Item> items;
    private List<Item> displayedItems;

    public MyAdapter(List<Item> items) {
        this.items=items;
        this.displayedItems=items;
    }

    public filter(String query) {
        if(query.isEmpty()) {
            displayedItems=items;
        } else {
            displayedItems=new ArrayList<Item>();
            for (Item item : items) {
                displayedItems.add(...) //add items matching your query
            }
        }
        notifyDataSetChanged();
    }

    //...
    //NOTE: we use displayedItems in getSize(), getView() and other callbacks 
}

Upvotes: 0

Related Questions