ArrayAdapter Filtering Filters Showing Incorrect Elements

I have extended the ArrayAdapter for my custom class. My class overrides toString() and returns the field that 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 works (the correct number of elements is returned), but these are incorrect elements. It always shows the first items in the list, and not those that match the search ...

+4
source share
1 answer

I had exactly the same problem, and I would suggest (for example, me) that you did not read the documentation when trying to implement this.

I passed an array of arrays to an ArrayAdapter and then passed an array to a super constructor.

I made a mistake: I saved the reference to the passed array and used an array that , to draw the elements 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 have to do is called getItem() in the ArrayAdapter class, for example.

  MyType myType = getItem( position ); 

Which recorded it beautifully and in retrospect is pretty obvious.

- (e)

+10
source

Source: https://habr.com/ru/post/1446685/


All Articles