Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
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 25-Feb-13 4:43am
miktg598
Edited 25-Feb-13 5:41am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You might want to try adding this line just after you setup your listView
 
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    listView = (ListView) findViewById(R.id.list);
    ..
add this
//enables filtering for the contents of the given ListView
  listView.setTextFilterEnabled(true);
 
Also Change this
 
  @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.objAdapter.getFilter().filter(cs);
    }
 
To
 
 @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
  Permalink  
Comments
miktg at 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 at 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 at 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
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100