title: how to apply filter in my custom listview in android?
body: <p>In my android app I want to apply a filter on the listview. Each row of the list shows the contact name, type of contact number, contact number etc.
  How can I apply the filter on the list, so when I enter the name of the person on edit text the list view must be refreshed on text change? I am using the another class to populate the list.  </p>
  
  <p><strong>Here is my code.</strong></p>
  
  <p>AllContactsActivity  class retrieve the contact from the phone.</p>
  
  <pre><code>public class AllContactsActivity extends ListActivity implements
              android.view.View.OnClickListener, OnItemClickListener {
          EditText sc;
          String name, phonetype;
          ImageButton favourites, contacts, keypad, recent, about;
          int arr, key;
          Cursor tcur;
          int[] typecount, count, id;
          ListView lv;
          ListViewAdapterContacts lva;
          String[] names, numbers, typeinfo, contactinfo, types;
          Integer[] sortlist;
          TreeMap&lt;Integer, String&gt; sorted_set;
          LinkedHashMap&lt;Integer, String&gt; sortedMap;
          ContentResolver tcr;
          HashMap&lt;Integer, String&gt; numbhashmap;
          ArrayAdapter&lt;String&gt; adapter = null;
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
  
              LayoutParams params = new RelativeLayout.LayoutParams(
                      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              LinearLayout mainLayout = new LinearLayout(this);
              mainLayout.setOrientation(LinearLayout.VERTICAL);
              LayoutInflater layoutInflater = getLayoutInflater();
              mainLayout.addView(layoutInflater.inflate(R.layout.allcontacts, null));
              mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
              this.addContentView(mainLayout, params);
  
              //configureBottomMenu();
              getphones();//get the cantact number
              getContacts();//get contact name,type,id
      //names,types,number,id are populated by above two methods.
  
              lv = new ListView(getApplicationContext());
              lv = (ListView) findViewById(android.R.id.list);
      //ListViewAdapterContacts class is specified later in the code which populate the list view
  
              lva = new ListViewAdapterContacts(this, names, types, numbers, id);
  
  
              lv.setAdapter(lva);
              lv.setTextFilterEnabled(true);
              lv.setOnItemClickListener(this);
  
              sc=(EditText)findViewById(R.id.searchcontact);
              sc.addTextChangedListener(textwatcher);
  
  
          }// on create
          @Override
          protected void onDestroy() {
              super.onDestroy();
              sc.removeTextChangedListener(textwatcher);
          }
  
          private TextWatcher textwatcher = new TextWatcher() {
  
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  // TODO Auto-generated method stub
  
              }
  
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
                  // TODO Auto-generated method stub
  
              }
  
              public void afterTextChanged(Editable s) {
                  // TODO Auto-generated method stub
                  AllContactsActivity.this.lva.getFilter().filter(s.toString());
                  lva.notifyDataSetChanged();
  
              }
          };
  
  
      }
  </code></pre>
  
  <p><strong>Here is ListViewAdapterContacts.java</strong></p>
  
  <pre><code>public class ListViewAdapterContacts extends BaseAdapter implements Filterable{
  
      Activity context;
      String[] names;
      String[] types;
      String[] numbers;
      int[] id;
      public ListViewAdapterContacts(Activity context, String[] names, String[] types, String[] numbers, int[] id) {
          // TODO Auto-generated constructor stub
  
      this.context=context;
      this.names=names;
      this.types=types;
      this.numbers=numbers;
      this.id=id;
      }
  
      public int getCount() {
          // TODO Auto-generated method stub 
          if(names==null){
              return 0;
          }
          else{
              return names.length;
          }
      }
  
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return null;
      }
  
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      public class viewHolder {
          TextView top;
          TextView bottom;
          TextView downside;
          TextView base;
      }
  
        public View getView(int position, View convertView, ViewGroup parent) {
          // TODO Auto-generated method stub
          viewHolder holder;
          if(convertView==null){
  
          LayoutInflater inflator=context.getLayoutInflater();
          convertView=inflator.inflate(R.layout.textviewonly,null);
  
          holder=new viewHolder();
          holder.top=(TextView)convertView.findViewById(R.id.toptext);
          holder.bottom=(TextView)convertView.findViewById(R.id.bottomtext);
          holder.downside=(TextView)convertView.findViewById(R.id.lowest);
          holder.base=(TextView)convertView.findViewById(R.id.baseid);
          convertView.setTag(holder);
          }else{
              holder=(viewHolder)convertView.getTag();
          }
          holder.top.setText(names[position]);
          holder.bottom.setText(types[position]);
          holder.downside.setText(numbers[position]);
          holder.base.setText(""+id[position]);
  
  
          View v=(View)convertView;
          //return convertView;
          return v; 
      }
  
  
      public Filter getFilter() {
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>