2012-05-02 47 views
0

搜索過濾器這是我的代碼片段:爲一個ArrayList

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箱變。 假設我輸入「A」,它會過濾其中包含A的所有項目。我只想檢索並列出「以」開頭的項目「A.我該怎麼做?

+0

檢查谷歌的番石榴庫:http://docs.guava-libraries.googlecode.com/git-history/v12.0/javadoc/com/google/common/collect/Collections2.html#filter(java .util.Collection,com.google.common.base.Predicate) – kofemann

+0

http://stackoverflow.com/questions/10383219/android-search-from-large-arraylist/10396356#10396356 –

回答

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 

} 
}