Android UI How to - Enable filtering support on ListView








You can Enable filtering support on ListView.

Example

/*from  w  w  w  .  j a v a 2  s . c  om*/
package com.java2s.myapplication3.app;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity  {
    String[] presidents = {
            "XML",
            "HTML",
            "CSS",
            "Java",
            "Javascript"
    };

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

        ListView lstView = getListView();

        lstView.setTextFilterEnabled(true);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, presidents));
    }

    public void onListItemClick(
            ListView parent, View v, int position, long id)
    {
        Toast.makeText (this,
                "You have selected " + presidents [position],
                Toast.LENGTH_SHORT).show();
    }

}
null




Note

To programmatically get a reference to the ListView object, you use the getListView() method, which fetches the ListActivity's list view.

Filtering allows user to type on the keypad and the ListView will automatically filter the items to match what was typed.