how to implement search in custom listview in android?

后端 未结 2 423
悲哀的现实
悲哀的现实 2021-01-07 01:37

I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles bu

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-07 01:45

    Android has an built in search bar feature .

    • You can use this search bar to get input from the user .Can refer to this link link2
    • Create an async task which will query your database/data to fetch matching results and populate the listview again

    Step 1 Add the following in your manifest :

    
                
                    
                        
                    
                    
                        
                    
    
                    
                
    
                
            
    

    Step2

    onSearchRequested(); will call your search bar .You can add this in on click of your search button.

    Step3 Simply call this function in your activity's on create :

    private void handleIntent(Intent intent) {
    
             if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                myList.clear();
    
                String query = intent.getStringExtra(SearchManager.QUERY);
    
                new getQueryResultAndPopulateListTask(this, query).execute();
            }
    
        }
    

提交回复
热议问题