2

hi i am new to android what i did is creating a list with strings and implementing the search using edit text,my code for this is as fallows

 ((EditText)findViewById(R.id.EditText01)).setOnKeyListener(new OnKeyListener() 
    {

  public boolean onKey(View v, int keyCode, KeyEvent event)
  { String enteredText = ((EditText)findViewById(R.id.EditText01)).getText().toString();

      Iterator<String> strIt = strList.iterator();
    while(strIt.hasNext()){
      String str = strIt.next();

      if(str.startsWith(enteredText)){


       match.add(str);

     } 
     }

     } 

it works when focus is changed form edit text,but i need to work it like quick search box.when ever i enter a letter in edit text,the matching words will be displayed.how can i done this.pls post some code.Thank u in advance.

2 Answers 2

3

Try implementing TextWatcher interface.

It has three methods which you need to override.

public void afterTextChanged(Editable s) {
        
        Log.v("afterTextChanged","here");
    }
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        
        Log.v("beforeTextChanged","here");
    }
    
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

I think this will do your work.

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

Comments

0

Take a Look at AutoCompleteTextView.

Basically you can create an ArrayAdapter or a SimpleAdapter that contains your Strings. Then just replace your EditText with the AutoCompleteTextView and automagically ... you get the typeahead behavior that you describe.

2 Comments

but i need to display all the items that come in the hint in a text view.Is it possible to do it
AutocompleteTextView will create TextView list that has all the possible words that match your strings. Is this what you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.