Search View without having to click enter to show the result

Asked

Viewed 228 times

2

made a search on my list however it needs me to click enter to complete the search and show the results. I would like the result to come automatically when typing. Thank you.

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));

  } 

  ........ 
  • maybe it doesn’t work because cell phones have no buttons, try to use the event touch

1 answer

3


Use the method addTextChangedListener Edittext instead of 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) {}
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.