0

I have an EditText, I use it to allow people to search the database. Now I have everything that works well, however, when someone enters something into the EditText my ListView is not updating the way I want it to.

I want it so that whenever someone enters anything into the EditText it updates the ListView. However, it seems like nothing is happening when I enter text into it.

Heres my code:

@Override protected void onResume() { searchText = (EditText)findViewById(R.id.search_text); ImageView searchView = (ImageView)findViewById(R.id.button_exercises_search); ImageView searchDoneView = (ImageView)findViewById(R.id.button_exercises_search_done); if (isSearching) { searchView.setVisibility(View.GONE); searchDoneView.setVisibility(View.VISIBLE); searchText.setVisibility(View.VISIBLE); } else { searchView.setVisibility(View.VISIBLE); searchDoneView.setVisibility(View.GONE); searchText.setVisibility(View.GONE); } searchText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { String query = searchText.getText().toString(); if (query.length() > 0) { cursor = datasource.fetchFilterExercises(dayDataID, query); } else { cursor = datasource.fetchAddExercises(dayDataID); } dataAdapter.changeCursor(cursor); return false; } }); super.onResume(); } 

The 2 buttons are just icons that, when clicked, show/hide the EditText for searching. I know that the datasource properly grabs the data I want, etc. It just seems like nothing happens at all when I enter text into the EditText searchText. Should I move this into onCreate()? Or?

3 Answers 3

5

You could try to use searchText.addTextChangedListener(...) and move all you logic to it's afterTextChanged(...) callback. According to documentation of View.OnKeyListener() :

Register a callback to be invoked when a hardware key is pressed in this view. Key presses in software input methods will generally not trigger the methods of this listener.

Hope this would help you!

Sign up to request clarification or add additional context in comments.

4 Comments

How do u know which key was pressed ie if yoiu press Space, delete etc?
@jonney i know nothing about pressed keys, i know only how text was changed through afterTextChanged callback parametr.
But that is what the OP wants? to know what keys were pressed, Your answer only sees what characters where entered in the textfield, not what actual keys pressed where made as it cant track keys like space, enter , DEL etc
@jonney please read the question carefully "whenever someone enters anything into the EditText it updates the ListView". I haven't try to fix his code, i gave him a different solution to get what he wants.
1

You could try to use ((EditText) findViewById(R.id.editText)).addTextChangedListener(...) and move all you logic

((EditText) findViewById(R.id.editText)) .addTextChangedListener(new TextWatcher() {

 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String query = searchText.getText().toString(); if (query.length() > 0) { cursor = datasource.fetchFilterExercises(dayDataID, query); } else { cursor = datasource.fetchAddExercises(dayDataID); } dataAdapter.changeCursor(cursor); } @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 } }); 

Hope this would help you..

Comments

0

As u are using the ListView might be using the Adapter, If So then add this Line mAdapter.notifyDataSetChanged();

1 Comment

the listview gets updated by changing the cursor since it uses a simplecursoradapter. but thank you for trying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.