搜索过滤器的Arraylist

时间:2012-05-02 10:24:27

标签: android search android-arrayadapter android-edittext

这是我的代码段:

public void onTextChanged(CharSequence changed_character, int start, int before, int count) 
    {
        if(search_adapter != null)
        {
            search_adapter.getFilter().filter(changed_character.toString().trim());
        }
    }

所以,我在这里根据我的edittext框中更改的文本过滤了arraylist。 假设我输入“A”,它会过滤掉包含A的所有项目。我只想检索并列出“以”开头的项目A.我该怎么做?

1 个答案:

答案 0 :(得分:0)

<AutoCompleteTextView android:id="@+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
/>


public class AndroidAutoCompleteTextView extends Activity implements TextWatcher{

AutoCompleteTextView myAutoComplete;
String item[]={
  "January", "February", "March", "April",
  "May", "June", "July", "August",
  "September", "October", "November", "December"
};

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       myAutoComplete = (AutoCompleteTextView)findViewById(R.id.myautocomplete);

       myAutoComplete.addTextChangedListener(this);
       myAutoComplete.setAdapter(new ArrayAdapter<String>(this,         android.R.layout.simple_dropdown_item_1line, item));

   }

@Override
public void afterTextChanged(Editable arg0) {
 // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
  int after) {
 // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
 // TODO Auto-generated method stub

}
}