Filtering list view and getting correct onclick item
Asked Answered
C

5

6

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

Copilot answered 6/12, 2012 at 2:56 Comment(3)
Where are you refreshing (filtering) your data?Younker
Get the right index by 'id' ?Eponymous
while filtering itself , get all the filtered data values(say if it has name and id , have 2 arrays ) and add them to new arrays respectively and set newly added array to the list view by invalidating the former views in list . so now the onclick will wok fine even if their positions are changed .Slicer
I
0

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

Irrigation answered 6/12, 2012 at 9:58 Comment(4)
Yes, stupid mistake. Thanks for making me realize, pois.get(id) is searching in the original list and not the filtered one.Copilot
What will be in my case........pls help.........#20607266Cahn
Hi, cannot open the given link for the solutionBenzyl
Just copy and paste the link, I don't know why it doesn't work!Irrigation
D
4
 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

Doorstone answered 22/10, 2013 at 13:35 Comment(1)
did you try this,let me knowDoorstone
I
0

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

Irrigation answered 6/12, 2012 at 9:58 Comment(4)
Yes, stupid mistake. Thanks for making me realize, pois.get(id) is searching in the original list and not the filtered one.Copilot
What will be in my case........pls help.........#20607266Cahn
Hi, cannot open the given link for the solutionBenzyl
Just copy and paste the link, I don't know why it doesn't work!Irrigation
G
0

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 
}
Gassaway answered 6/12, 2012 at 10:15 Comment(0)
S
0

You can try:

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

in your adapter

Sainted answered 6/12, 2012 at 10:15 Comment(1)
I added what you said inside my CustomList adapter which is defined like this: public class CustomListAdapter extends ArrayAdapter<Object> implements Filterable{ ...} but didn't work :(Copilot
B
0

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.

Brotherinlaw answered 25/1, 2016 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.