Listview Search in Android Studio

Introduction

 
This article explains how to search for Item in a ListView.
 
First, you will create an XML file and use an EditText and ListView. The EditText is used to search for an item and the ListView is used to show the list of items.
 
Now you will create a Java file and write code to show an item in a ListView.
  1. String products[] = {"Dell Inspiron""HTC One X""HTC Wildfire S""HTC Sense""HTC Sensation XE",  
  2.                                                 "iPhone 4S""Samsung Galaxy Note 800",  
  3.                                                 "Samsung Galaxy S3""MacBook Air""Mac Mini""MacBook Pro"};  
  4. myListView = (ListView) findViewById(R.id.editlist_view);  
  5. inputSearch = (EditText) findViewById(R.id.itemSearch);  
  6. myAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);  
  7. myListView.setAdapter(myAdapter); 
After show item in a list view, you will write code to perform a search in a list view
  1. inputSearch.addTextChangedListener(new TextWatcher() {                      
  2.  @Override  
  3.  public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {  
  4.  MainActivity.this.myAdapter.getFilter().filter(cs);  
  5.   }                      
  6.   @Override  
  7.   public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {   
  8.                      }  
  9.                      @Override  
  10.                      public void afterTextChanged(Editable arg0) {  
  11.                      } 
Step 1
 
Create a XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     android:orientation="vertical" >    
  6.        
  7.     <!-- Editext for Search -->    
  8.     <EditText android:id="@+id/itemSearch"    
  9.        android:layout_width="fill_parent"    
  10.        android:layout_height="wrap_content"    
  11.        android:hint="Search products.."    
  12.        android:inputType="textVisibleword"/>    
  13.      
  14.        <!-- List View -->    
  15.     <ListView    
  16.         android:id="@+id/editlist_view"    
  17.         android:layout_width="fill_parent"    
  18.         android:layout_height="wrap_content" />    
  19.      
  20. </LinearLayout>   
Step 2
 
Create another XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.          
  7. <TextView android:id="@+id/product_name"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:padding="10dip"  
  11.         android:textSize="16dip"  
  12.         android:textStyle="bold"/>     
  13.    
  14. </LinearLayout> 
Step 3
 
First, you will create an array, String Product[], that is of String type to store items that we want to display in the ListView. We this array as an argument to the ArrayAdapter constructor that returns the object of ArrayAdapter. After getting the object you will this as an argument to the setAdapter method to set the list of items in the ListView.
 
Now you will set the EditText to the addTextChangelistener to do the search on the EditText contents.
 
Create a Java file and write this:
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.text.Editable;  
  4. import android.text.TextWatcher;  
  5. import android.widget.ArrayAdapter;  
  6. import android.widget.EditText;  
  7. import android.widget.ListView;  
  8. import java.util.ArrayList;  
  9. import java.util.HashMap;  
  10.    
  11. public class MainActivity extends Activity {  
  12.         
  13.        private ListView myListView;  
  14.        ArrayAdapter<String> myAdapter;  
  15.        EditText inputSearch;  
  16.        ArrayList<HashMap<String, String>> productList;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         String products[] = {"Dell Inspiron""HTC One X""HTC Wildfire S""HTC Sense""HTC Sensation XE",  
  22.                                                 "iPhone 4S""Samsung Galaxy Note 800",  
  23.                                                 "Samsung Galaxy S3""MacBook Air""Mac Mini""MacBook Pro"};  
  24.         myListView = (ListView) findViewById(R.id.editlist_view);  
  25.         inputSearch = (EditText) findViewById(R.id.itemSearch);  
  26.         myAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);  
  27.         myListView.setAdapter(myAdapter);  
  28.         inputSearch.addTextChangedListener(new TextWatcher() {  
  29.                      @Override  
  30.                      public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {  
  31.                            MainActivity.this.myAdapter.getFilter().filter(cs);  
  32.                      }  
  33.                      @Override  
  34.                      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,  
  35.                                   int arg3) {  
  36.                      }  
  37.                      @Override  
  38.                      public void afterTextChanged(Editable arg0) {  
  39.                      }  
  40.               });  
  41.     }  
Step 4
 
When you will run your project:
 
1.jpg
 
Step 5
 
When you will search for the particular item:
 
2.jpg
 
Step 6
 
3.jpg
 
So in this article, you learned how to search for an item for a ListView.


Similar Articles