views:

550

answers:

2

I have created a layout which includes a ListView.

The data shown within this ListView isn't too complicated, it's mostly an Array which is passed when the extended Activity is started.

The rows themselves exist out of an icon and a text, thus an ImageView and a TextView. To fill up the ListView I use an ArrayAdapter simply because an Array is passed containing all the text-items that should be shown.

Now I'd like to actually be able to filter those, thus I found the android:textFilterEnabled paramater to add on the ListView xml declaration... Now a search field is shown nicely but when I enter some letters it won't filter but it will simply delete the whole list. I found out that that's because the textfilter has no idea what it should filter.

So now my question is : I know I need to tell the textfilter what it should filter, I also still have my array filled with the text that should get filtered so how do i couple those two?

I have seen examples extending a CursorAdapter, but again, I don't have a Cursor, I don't want to do calls to a DB I want to re-utilize my Array with data and obviously the ArrayAdapter itself so that the data will be represented decently on screen (i.e with my ImageView and TextView layout).

How can this be done?

A: 

Hi,

I was able to use an ArrayAdapter with "String"s for the generics parameter (i.e. I create it with ArrayAdapter<String> ... = new ArrayAdapter<String>(...)), in a ListView and call setTextFilterEnabled and setFilterText programmatically to filter them out. Below is how I did it in a ListActivity:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // This array list will be updated each time.
    final ArrayList<String> data = new ArrayList<String>();
    data.add("apple");
    data.add("orange");
    data.add("tomato");
    dataAdapter = new ArrayAdapter<String>(this, R.layout.item,
        R.id.itemName, data);

    setListAdapter(dataAdapter);
    ListView view = getListView();
    view.setTextFilterEnabled(true);
    view.setFilterText("ap");
}

I also did try it with a non-string based adapter and you can get the resulting code from here. In this case, I created an object called Item, which is what I want to put in my ListView and ArrayAdapter. It filters against whatever is returned by the object's toString method. In the example, I toString filter against the name of the item. Note that unlike the above case, I setted the android:textFilterEnabled property in my view's XML file instead of calling setTextFilterEnabled, just as you had described.

Klarth
ok, I seey ou actually are filtering directly with view.setFilterText("ap");but how do I retrieve the "ap" letters from the standard Android's TextFilter ? I do get it onscreen, but afterwards I have to know what the user is entering, how to achieve that ?
TiGer
+1  A: 

Your custom adapter has to implement the Filterable interface, override getFilter() and return an instance of a class which extends Filter in getFilter().

The way I did it was to create an instance of the filtering class and store it in a final instance variable inside my adapter and return that when getFilter() is called.

Have a look at the ArrayAdapter source code to get a better idea of how to do the filtering yourself.

Al