Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayAdapter stock filtering showing wrong elements

I have extended ArrayAdapter for my custom class. My class overrides toString() and returns the field which i want to use for search queries.

This is my filter code:

productItemAdapter.getFilter().filter(filterText.toLowerCase());

this is my toString() code:

return name.toLowerCase();

Filtering is working (the correct number of elements is returned), but it's the wrong elements. It always shows the the first elements on the list instead of the ones that match the search...

like image 822
Paul Avatar asked Dec 26 '22 14:12

Paul


1 Answers

I had exactly the same issue and I would assume (like me) that you didn't read the documentation when trying to implement this.

I was passing in my array of objects to the ArrayAdapter and subsequently passing the array onto the super constructor.

The mistake I made was: I stored a reference to the passed in array and used that array to draw the items in the getView() method:

 public SimpleAdapter( Context context, List< MyType > values )
 {
    super( context, R.layout.rowlayout, values );
    this.context = context;
    this.values = values;
 }

  @Override
  public View getView( int position, View convertView, ViewGroup parent )
  {
    // ...
    MyType myType = values.get( position );
    // ...
  }

What I should have done is called getItem() on the ArrayAdapter class e.g.

  MyType myType = getItem( position );

Which fixed it beautifully and in hindsight is pretty obvious.

-(e)

like image 113
sweetlilmre Avatar answered Jan 13 '23 15:01

sweetlilmre