Black Smith Black Smith - 1 month ago 22
Android Question

filter is not working with space android

Hi i have one editbox in that if user type the filtering should be automatically done in listview...

from below code all is working fine but if i put space in character the listview does not display any entry..

below is my code

adapter = new SimpleAdapter(PredefinedLocation.this, fillMaps, R.layout.list_item_deal, new String[] { "name" }, new int[] { R.id.name });
// Adding data into listview
lv.setAdapter(adapter);
edtSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
lv.setTextFilterEnabled(true);
lv.setFilterText(s.toString().trim());
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(s.length()==0){
lv.clearTextFilter();
}
}
});


Like if i write "The" the result will be "The Local", "The Adams"

but if i write "The Local" the result will be blank.... because of space in "The" & "Local" :(

Answer

This problem is occurring because you are using SimpleAdpater, try to use ArrayAdapter it will solve your problem. I do not know why this is!

Check out this

http://www.talkandroid.com/android-forums/android-development-answers-tutorials-code-snippets/2534-diferent-filter-behavior-simpleadapter-arrayadapter.html

try to put your code like this way, I have tested it works perfectly

  adapter = new ArrayAdapter<String>(PredefinedLocation.this, R.layout.list_item_deal, R.id.name, fillMaps);

    lv.setAdapter(adapter);
    edtSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            lv.setTextFilterEnabled(true);
            lv.setFilterText(s.toString().trim());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            PredefinedLocation.this.adapter.getFilter().filter(s.toString().trim());
            /*if(s.length()==0){
                lv.clearTextFilter();
            }*/
        }
    });