Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I got some sample code and i wanted to add search filter on it, after you type any letter to filter it won't show the filtered contacts or it won't update the listview, can anyone say something on it, thanks.
import ........

   ContactAdapter objAdapter;
   private ListView listView;
   EditText inputSearch;
   private List<contactbean> list = new ArrayList<contactbean>();

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);       

listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    listView = (ListView) findViewById(R.id.list);

    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext()) {
        String name = phones.getString(phones           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        String phoneNumber = phones.getString(phones                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        ContactBean objContact = new ContactBean();
        objContact.setName(name);
        objContact.setPhoneNo(phoneNumber);         
        list.add(objContact);

    }
    phones.close();

   objAdapter = new ContactAdapter(MainActivity.this, R.layout.alluser_row, list);
   listView.setAdapter(objAdapter);
      if (null != list && list.size() != 0) {
        Collections.sort(list, new Comparator<contactbean>() {

            @Override
            public int compare(ContactBean lhs, ContactBean rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
       } else {
    showToast("No Contact Found!!!");
 }

    inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.objAdapter.getFilter().filter(cs);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)                   {
        // TODO Auto-generated method stub   
    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
    }
});
  }
Posted
Updated 25-Feb-13 4:41am
v2

1 solution

You might want to try adding this line just after you setup your listView

Java
inputSearch = (EditText) findViewById(R.id.inputSearch);
listView = (ListView) findViewById(R.id.list);
..

add this
Java
//enables filtering for the contents of the given ListView
  listView.setTextFilterEnabled(true);


Also Change this

Java
@Override
  public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
      // When user changed the Text
      MainActivity.this.objAdapter.getFilter().filter(cs);
  }


To

Java
@Override
   public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
       // When user changed the Text
       objAdapter.getFilter().filter(cs.toString());
   }


I also found in the emulator I had to press a letter and return to get it to work but then my emulator is currently in Chinese for some reason, don't ask....

Hope it helps

/Darren
 
Share this answer
 
Comments
miktg 26-Feb-13 9:28am    
Thanks but not still working, when you type one letter on inputSearch (EditText) all contacts will be gone even if it contains a match and the list will be listed back when you make the inputsearch is empty.
Darren_vms 26-Feb-13 15:11pm    
What you describe can be normal, the filter is simple. If you have a list of names and none of them begin with "z" type that in and they will all disappear. This type of filter will only match the beginning of words.

example :

one
two
three

type r nothing will show
type t (two and three should be left)
type th (three should be left)

if you want something better than this simple filter you need to create your own adapter and write the code.

Or you may just have a bug in the implementation :-)

I will tidy up what I have and send you a link.

/Darren
miktg 27-Feb-13 1:51am    
@Darren, to answer the above comment based on your example when I type t nothing is showed up, thank you so much i will be waiting for your link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900