简体   繁体   中英

How to search list view with Edit Text in Android?

I want to search ListView by writing something inside the EditText . like if I write first to words "Su" so the content in ListView with first two words as "Su" should come on top.

This is my code:

Sear = (AutoCompleteTextView) findViewById(R.id.Search);
    Sear.setThreshold(1);

    Sear.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("Text ["+s+"]");

            sdAdapter.getFilter().filter(s.toString());                           
        }

        @Override

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

Right now when I write something in my EditText the ListView dissapears. I am having 5 columns in my ListView . i want to search the name of that specific person and search the ListView with that name.

This is the Adapter:

sdAdapter = new StationDetails_Adapter(AllStations.this, R.layout.listview_row2 , Stationlist);

Searching in a ListView can be done with local SQLite DB. I suppose you shuld have knowledge about SQLite Database then follow the steps like:

1) Set ListView adapter and store all data in SQLite Database

2) write a simple query on DB end like: select * from YOUR_TABLE_NAME where column_name like '%+your_search_keyword+%'

3)execute this query while you are in onTextChanged() method.

4) use cursor to get all data against search keyword from SQLite DB and set the ListView adapter

5) If cursor do not return any data means you have no record against search keyword.

Other Way: Other than SQLite DB you can set adapter and filter on a specific search keyword like:

For Example:

    String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                    "iPhone 4S", "Samsung Galaxy Note 800",
                                    "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

            lv = (ListView) findViewById(R.id.list_view);
            inputSearch = (EditText) findViewById(R.id.inputSearch);

            // Adding items to listview
            adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
            lv.setAdapter(adapter);

inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);   
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub                          
    }
});

The above example is referred from Android Hive Tutorial

You have to implement filterable in your listAdapter. Below example does the same thing that you want.

http://www.androidbegin.com/tutorial/android-search-listview-using-filter/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM