Sudhanshu
Sudhanshu

Reputation: 755

Filtered ListView onItemClick returns Item at original position

I have a listview with custom rows being populated from an custom BaseAdaptor. On click of any row, I open a new Activity. Everything was working fine until I added the filter functionality to this list. When I search the list and THEN click on an Item, it doesn't open the activity associated with the filtered results. It opens up an Activity related to the Item at that position in Original list.

Eg. - Original List : AA, BA, CC, DA, ED, FF

Search : 'A' Filtered results: AA, BA, DA

But when I click on item DA it opens up the Activity for CC. Extremely irritating. I have called notifyDataSetChanged() on the adapter.

I've been stuck with this problem for sometime. I really dont know how to solve it. I didnt post the code because it's a whole lot of code and I really dont want to put everything here.

If someone can give me an idea of how to select the Item from the FILTERED list.. It'd b great.

Thanks! Tell me if something else is needed to understand my question!

Upvotes: 6

Views: 5512

Answers (3)

Kenart
Kenart

Reputation: 335

What you could do is that in your custom SearchViewAdapter (i.e) the class where you set (context,resource,Arraylist) you can create a method that stores the original ArrayList that is passed into the SearchViewAdapter and create a method that gets the original position of the selected object from listView:

 //in your custom SearchViewAdapter create methods store() and getPos() 

public void store(ArrayList<SearchViewClass>originalList){
   mylist=new ArrayList<>(originalList);
      }  //The store method should be called once in searchView Activity when you pass all needed values into your Arraylist            


public int getPos(Object obj){
    return mylist.indexOf(obj);
}

then in your SearchView Activity or anywhere you are implementing the searchView, on listView.setOnItemClickListener, you call the getpos(adapterView.getItemAtPosition(i)) method that takes the adapterView.getItemAtPosition(i) as a paramter. The getpos() method returns the index of the original object position regardless of the filtering caused by the searchView.Then you can switch() on the index,

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
 switch (searchViewAdapter.getPos(adapterView.getItemAtPosition(i))){
               case 0:...break}
}

Upvotes: 0

Sudhanshu
Sudhanshu

Reputation: 755

Thanks for replying all but I found the problem.

I hope this helps another person. With something this basic, it sometimes gets hard to find the error and we resort to much complicated ways!

To start my new activity, I was taking the item position from the custom Adaptor without overriding the getItem() function.

Object obj = myListAdapter.getItem(position);

I overrid the function

@Override
public Object getItem(int position) {
    return myList.get(position);
}

and voila! got the right Activity opening. (facepalm i know..) Thanks for your answers anyway!

Upvotes: 13

Arash GM
Arash GM

Reputation: 10395

This happens because your CustomAdapter use your first Cursor not the filtered one

you can use this in your Adapter definition :

public Cursor swapCursor(Cursor c1)
{
    super.swapCursor(c1);
    c = c1;
    notifyDataSetChanged();
    return c;

} 

and in your calling Activity use

YourCustomAdapter.swapCursor(FilteredCursor);

Upvotes: 0

Related Questions