1

I am trying to filter a gridview and for that I am entering the data in an editbox. It works great except that when I insert a space character in editbox it somehow doesn't recognize it and the result set comes empty. Please help

Here is my editbox listener:

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {}

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {}

    @Override
    public void onTextChanged(CharSequence cs, int start, int before, int count) {

        MainActivity.this.adapter.getFilter().filter(cs);

        }

});

I can't figure out the problem since it's working fine with other characters.

UeliDeSchwert
  • 1,146
  • 10
  • 23
Zonera
  • 178
  • 9

3 Answers3

1

Try using trim(). Hope it will work for you.I know you will need to modify this code.but it may give you some idea.

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

        MainActivity.this.adapter.getFilter().filter(search.getText().toString().trim());

    }

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

    }

    @Override
    public void onTextChanged(CharSequence cs, int start, int before,
            int count) {
        // TODO Auto-generated method stub

        }

});

If this doesnt work try second option

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
       String result = s.toString().replaceAll(" ", "");
    if (!s.toString().equals(result)) {
         ed.setText(result);
         ed.setSelection(result.length());
         // alert the user
    }

    }

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

    }

    @Override
    public void onTextChanged(CharSequence cs, int start, int before,
            int count) {


        }

});

I prefer first option using trim() to string text.

Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
  • Thank you for your help. It worked. But now it doesn't recognize the word after space. Like if I put "Fighting child labor" it only recognizes fighting and the space after it but doesn't recognize child. – Zonera Mar 31 '14 at 12:39
  • try adding maxlength to your edittext.Its just one suggestion,may not be perfect solution in your case. – Harshal Benake Mar 31 '14 at 17:18
  • @Zonera how did u solved ur issue listed in ur comment above?by max length? – KOTIOS Aug 05 '14 at 09:48
0

Move Your filter code to afterTextChanged() method.

For example:

search.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

        MainActivity.this.adapter.getFilter().filter(search.getText().toString());

    }

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

    }

    @Override
    public void onTextChanged(CharSequence cs, int start, int before,
            int count) {
        // TODO Auto-generated method stub

        }

});
Roman Black
  • 3,501
  • 1
  • 22
  • 31
0

I was able to solve this problem with the following code:

@Override
public void afterTextChanged(Editable editable) {

    String keywords = editable.toString();
    if (keywords.length() > 0){
        String ultimo = keywords.substring(keywords.length() - 1);
        if (ultimo.equals(" ")){
            youtubeSearchView.setText(editable.toString() + "\b");
            youtubeSearchView.setSelection(keywords.length());
        }
    } 
Til
  • 5,150
  • 13
  • 26
  • 34