scarhand
scarhand

Reputation: 4337

setOnKeyListener not working with EditText

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?

Upvotes: 0

Views: 5527

Answers (3)

Amit kumar
Amit kumar

Reputation: 1605

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..

Upvotes: 1

deepak825
deepak825

Reputation: 432

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

Upvotes: 0

Evos
Evos

Reputation: 3915

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!

Upvotes: 5

Related Questions