Listview Filtering in Android

Here I have an edit text and a Listview, and I will explain how we can filter the Listview according to values entered by the user in the edit text. First create the layout file for 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:background="@android:color/white"  
  6.         android:orientation="vertical" >  
  7.   
  8.         <RelativeLayout  
  9.             android:id="@+id/wrapper_counrty_search"  
  10.             android:layout_width="match_parent"  
  11.             android:layout_marginTop="10dp"  
  12.             android:layout_marginBottom="10dp"  
  13.             android:layout_height="wrap_content"  
  14.             android:padding="10dp" >  
  15.                 
  16.             <EditText  
  17.                 android:id="@+id/country_picker_search"  
  18.                 android:layout_width="match_parent"  
  19.                 android:layout_height="wrap_content"  
  20.                 android:hint="search"  
  21.                 android:paddingLeft="5sp"  
  22.                 android:paddingRight="5dp"  
  23.                 android:textCursorDrawable="@null"  
  24.                 android:textSize="14sp" />  
  25.         </RelativeLayout>  
  26.                   
  27.         <Lictview   
  28.             android:id="@+id/country_picker_Lictview "  
  29.             android:layout_width="match_parent"  
  30.             android:layout_height="0dp"  
  31.             android:layout_marginBottom="10dp"  
  32.             android:layout_marginLeft="10dp"  
  33.             android:layout_marginRight="10dp"  
  34.             android:layout_weight="1"  
  35.             android:divider="@android:color/transparent"  
  36.             android:dividerHeight="2dp" >  
  37.         </Lictview >  
  38.   
  39.     </LinearLayout>  
Bind the UI controls in the JAVA file,
  1. searchEditText = (EditText) findViewById(R.id.country_picker_search);  
  2. countryLictview = (Lictview ) findViewById(R.id.country_picker_Lictview );  
Now we need to create the Listview row like the following,
  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="wrap_content"  
  5.         android:orientation="vertical" >  
  6.             
  7.         <TextView  
  8.             android:id="@+id/country_title"  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:padding="10dp"  
  12.             android:paddingLeft="5dp"  
  13.             android:textStyle="bold"  
  14.             android:textColor="@android:color/black" />  
  15.         <TextView  
  16.             android:id="@+id/country_code"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:paddingLeft="10dp"  
  20.             android:text="asdasdasd"  
  21.             android:textColor="@android:color/black"  
  22.             android:textSize="12sp" />  
  23.     </LinearLayout>  
We have a JSON file that is in the raw folder of the Android application. Now create the List adapter like the following,
  1. public class PickerAdapter extends BaseAdapter  
  2. {  
  3.     private Context context;  
  4.     List < PickerItem > pickerItems;  
  5.     LayoutInflater inflater;  
  6.     /** 
  7.      * Constructor 
  8.      * 
  9.      * @param context 
  10.      */  
  11.     public PickerAdapter(Context context, List < PickerItem > contactList)  
  12.     {  
  13.         super();  
  14.         this.context = context;  
  15.         this.pickerItems = contactList;  
  16.         inflater = (LayoutInflater) this.context  
  17.             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  18.     }  
  19.     @Override  
  20.     public int getCount()  
  21.     {  
  22.         return pickerItems.size();  
  23.     }  
  24.     @Override  
  25.     public Object getItem(int arg0)  
  26.     {  
  27.         return null;  
  28.     }  
  29.     @Override  
  30.     public long getItemId(int arg0)  
  31.         {  
  32.             return 0;  
  33.         }  
  34.         /** 
  35.          * Return row for each country 
  36.          */  
  37.     @Override  
  38.     public View getView(int position, View convertView, ViewGroup parent)  
  39.         {  
  40.             View cellView = convertView;  
  41.             Cell cell;  
  42.             PickerItem contact = pickerItems.get(position);  
  43.             if (convertView == null)  
  44.             {  
  45.                 cell = new Cell();  
  46.                 cellView = inflater.inflate(R.layout.picker_list_row, null);  
  47.                 cell.contactName = (TextView) cellView.findViewById(R.id.country_title);  
  48.                 cell.contactNumber = (TextView) cellView.findViewById(R.id.country_code);  
  49.                 cellView.setTag(cell);  
  50.             }  
  51.             else  
  52.             {  
  53.                 cell = (Cell) cellView.getTag();  
  54.             }  
  55.             cell.contactName.setText(contact.getItemName());  
  56.             cell.contactNumber.setText(contact.getItemDetails());  
  57.             return cellView;  
  58.         }  
  59.         /** 
  60.          * Holder for the cell 
  61.          * 
  62.          */  
  63.     static class Cell  
  64.     {  
  65.         public TextView contactName;  
  66.         public TextView contactNumber;  
  67.     }  
  68. }  
We should also have the model classes like the following,
  1. public class PickerItem  
  2. {  
  3.     public String getItemName()  
  4.     {  
  5.         return itemName;  
  6.     }  
  7.     public void setItemName(String itemName)  
  8.     {  
  9.         this.itemName = itemName;  
  10.     }  
  11.     public String getItemDetails()  
  12.     {  
  13.         return itemDetails;  
  14.     }  
  15.     public void setItemDetails(String itemDetails)  
  16.     {  
  17.         this.itemDetails = itemDetails;  
  18.     }  
  19.     private String itemName = null;  
  20.     private String itemDetails = null;  
  21. }  
And one more model class,
  1. public class PickerList  
  2. {  
  3.     public ArrayList < PickerItem > getPickerList()  
  4.     {  
  5.         return pickerList;  
  6.     }  
  7.     public void setPickerList(ArrayList < PickerItem > pickerList)  
  8.     {  
  9.         this.pickerList = pickerList;  
  10.     }  
  11.     private ArrayList < PickerItem > pickerList = null;  
  12. }  
Now in the Main Activity we need to set the Adapter to this Listview like the following,
  1. adapter = new PickerAdapter(this, selectedCountryList);  
  2. countryLictview.setAdapter(adapter);  
  3. Now we need to set the edittext text change properties  
  4. for filtering  
  5. searchEditText.addTextChangedListener(new TextWatcher()  
  6. {  
  7.     @Override  
  8.     public void onTextChanged(CharSequence s, int start, int before,  
  9.         int count)  
  10.     {}  
  11.     @Override  
  12.     public void beforeTextChanged(CharSequence s, int start, int count,  
  13.         int after)  
  14.     {}  
  15.     @Override  
  16.     public void afterTextChanged(Editable s)  
  17.     {  
  18.         search(s.toString());  
  19.     }  
  20. });  
This is how the search method works.
  1. private void search(String text)  
  2. {  
  3.     selectedCountryList.clear();  
  4.     // boolean needToAdd = true;  
  5.     for (PickerItem country: allContactList)  
  6.     {  
  7.         if (country.getItemName()  
  8.             .toLowerCase(Locale.ENGLISH)  
  9.             .contains(text.toLowerCase()))  
  10.         {  
  11.             if (!selectedCountryList.contains(country))  
  12.             {  
  13.                 // needToAdd = false;  
  14.                 selectedCountryList.add(country);  
  15.             }  
  16.         }  
  17.     }  
  18.     adapter.notifyDataSetChanged();  
  19. }  
For reading files from the raw folder we need to have this method.
  1. private static PickerList readFileAsString(Context context)  
  2. throws java.io.IOException  
  3. {  
  4.     int id = R.raw.refered_by;  
  5.     InputStream inputStream = context.getResources()  
  6.         .openRawResource(  
  7.             id);  
  8.     BufferedReader reader = new BufferedReader(new InputStreamReader(  
  9.         inputStream));  
  10.     StringBuffer result = new StringBuffer();  
  11.     String line;  
  12.     while ((line = reader.readLine()) != null)  
  13.     {  
  14.         result.append(line);  
  15.     }  
  16.     reader.close();  
  17.     Gson gson = new GsonBuilder()  
  18.         .create();  
  19.     return gson.fromJson(result.toString(), PickerList.class);  
  20.     // return result.toString();  
  21. }  
This will return the values as model class.

See the screen shots below,





Please find the source code also.

 

Ebook Download
View all
Learn
View all