2012-03-07 70 views
0

這是我的自定義ArrayAdapter定製ArrayAdapter爲自動完成,顯示所有結果

public class CustomAdapater extends ArrayAdapter implements Filterable { 
    private ArrayList<String> mData; 
    private ArrayList<String> orig; 
    private ArrayList<String> suggestions; 
    LayoutInflater inflater; 
    @SuppressWarnings("unchecked") 
    public CustomAdapater(Context context, int textViewResourceId,ArrayList<String> al) { 
     super(context,textViewResourceId,al); 
     inflater = LayoutInflater.from(context); 
     mData = al; 
     orig = (ArrayList<String>)mData.clone(); 
     this.suggestions = new ArrayList<String>(); 
    } 

    @Override 
    public int getCount() { 
     return suggestions.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return suggestions.get(position); 
    } 

    @Override 
    public Filter getFilter() { 
     Filter myFilter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults filterResults = new FilterResults();    
       if(constraint != null) { 
        suggestions.clear(); 
        for(String s:orig){ 
         if(s!=null && s.contains(constraint)) 
         suggestions.add(s); 
        } 
        filterResults.values = suggestions; 
        filterResults.count = suggestions.size(); 
       } 
       return filterResults; 
      } 

      @SuppressWarnings("unchecked") 
      @Override 
      protected void publishResults(CharSequence contraint, FilterResults results) { 
       mData = (ArrayList<String>)results.values; 
       notifyDataSetChanged();    
      } 
     }; 
     return myFilter; 
    } 
} 

我申請這自動完成場。默認情況下以搜索字符串開頭的自動完成搜索列表。我想要包含搜索字符串的列表。

回答

0

對於你想我想你應該寫你自己的代碼..因爲這是不可能的默認自動填充字段的功能... 想這是你正在使用的自動完成字符串數組..

static final String[] COUNTRIES = new String[20]; 

,它是有20個國家的..然後嘗試搜索這樣的..

for(int i=0;i<20;i++) 
{ 
int index=COUNTRIES[i].index(autocomplete.getText().toString);//<---- here autocomplete is the autocomplete textview 
if (index >-1) 
{ 
lst.add(COUNTRIES[i])<----- lst is the arraylist of strings you should create 
} 
} 
runOnUiThread(new Runnable(){ 
public void run() { 
for(int k=0;k<lst.size();k++) 
{ 
adapter.add(lst[k]) 
} 
adapter.notifyDataSetChanged(); 
} 
} 
); 

運行這段代碼在另一個線程時autocomplete.getText()。長度> 1(或任何你想要的threshould長度)...我想你會得到要求ired result .. =]

+0

如果這解決了您的問題或幫助..接受答案... =] – 5hssba 2012-03-14 09:37:04