开发者

how to implement search function in android

开发者 https://www.devze.com 2023-01-20 13:13 出处:网络
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

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

      Itera开发者_如何学Gotor<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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消