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. <RelativeLayout
  8. android:id="@+id/wrapper_counrty_search"
  9. android:layout_width="match_parent"
  10. android:layout_marginTop="10dp"
  11. android:layout_marginBottom="10dp"
  12. android:layout_height="wrap_content"
  13. android:padding="10dp" >
  14. <EditText
  15. android:id="@+id/country_picker_search"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:hint="search"
  19. android:paddingLeft="5sp"
  20. android:paddingRight="5dp"
  21. android:textCursorDrawable="@null"
  22. android:textSize="14sp" />
  23. </RelativeLayout>
  24. <Lictview
  25. android:id="@+id/country_picker_Lictview "
  26. android:layout_width="match_parent"
  27. android:layout_height="0dp"
  28. android:layout_marginBottom="10dp"
  29. android:layout_marginLeft="10dp"
  30. android:layout_marginRight="10dp"
  31. android:layout_weight="1"
  32. android:divider="@android:color/transparent"
  33. android:dividerHeight="2dp" >
  34. </Lictview >
  35. </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. <TextView
  7. android:id="@+id/country_title"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:padding="10dp"
  11. android:paddingLeft="5dp"
  12. android:textStyle="bold"
  13. android:textColor="@android:color/black" />
  14. <TextView
  15. android:id="@+id/country_code"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:paddingLeft="10dp"
  19. android:text="asdasdasd"
  20. android:textColor="@android:color/black"
  21. android:textSize="12sp" />
  22. </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.