Search View without needing to click enter to appear the result

2

I made a search in my list however it needs me to hit enter to complete the search and show the results. I would like the result to come automatically when typing. Appreciate.

My code:

........ 

public class ListClientes extends AppCompatActivity implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {
ListView lista;
ArrayList<Cliente> clientes;
EditText search;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_clientes);

    lista = (ListView) findViewById(R.id.listview);
    search = (EditText) findViewById(R.id.search);

    lista.setOnItemLongClickListener(this);
    lista.setOnItemClickListener(this);

    atualizar(null);

    search.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            atualizar(null);
            return false;
        }
    });
}


public void atualizar(View view) {
    ClienteDao cliDao = new ClienteDao();

    clientes = cliDao.getListagem(" where nome like '" + search.getText().toString() + "%'");
    lista.setAdapter(new ClienteAdapter(getBaseContext(), clientes));

  } 

  ........ 
    
asked by anonymous 21.02.2016 / 03:02

1 answer

3

Use the addTextChangedListener method of EditText when instead of the setOnKeyListener, as in the example:

search.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
        atualizar(null);
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
    
21.02.2016 / 06:23