2011-05-19 72 views
5

我已經在這幾天拉我的頭髮,我試圖在android中設置一個autocompletetextview,其中用戶輸入一個鍵和自動完成建議是值,但我已經嘗試了10種不同的方式現在,擴展了BaseAdapter,SimpleAdapter和現在的ArrayAdapter,並且我通過調試器注意到我的結果集很好,但是我真的不知道我應該在代碼的publishResults()部分中做什麼。第一個參數是自定義使用以下XML autocompletetextview控制:覆蓋android自動完成文本視圖中的filterresults?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <TextView android:id="@+id/txtInnerView2" android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
     </TextView> 
</LinearLayout> 

和類看起來是這樣的:

public class NewArrayAdapter<T> extends ArrayAdapter implements Filterable { 
    ArrayList<String> allWords; 
    ArrayList<String> resultWords; 
    String value[] = { "Awesome", "Bear", "Cat", "Dog" }; 
    String key[] = { "A", "B", "C", "D" }; 

    public NewArrayAdapter(Context context, int resource, int textViewResourceId) { 
     super(context, resource, textViewResourceId); 
     // TODO Auto-generated constructor stub 
     allWords = new ArrayList<String>(); 
     resultWords = new ArrayList<String>(); 
    } 

    @Override 
    public Filter getFilter() { 
     Filter custom_filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults f = new FilterResults(); 
       if (constraint != null) { 
        ArrayList<String> lstResults = new ArrayList<String>(); 
        for (int x = 0; x < key.length; x++) { 
         if (key[x].startsWith(constraint.toString().toUpperCase())) { 
          lstResults.add(value[x]); 
         } 
        } 
        f.values = lstResults; 
        f.count = lstResults.size(); 
       } 
       return f; 
      } 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       resultWords.clear(); 
       if (results.count > 0) { 
        resultWords.addAll((Collection<? extends String>) results.values); 
        notifyDataSetChanged(); 
       } else { 
        notifyDataSetInvalidated(); 
       } 
      } 
     }; 
     return custom_filter; 
    } 
} 

INT構造, 公共NewArrayAdapter(上下文的背景下,INT資源,INT textViewResourceId,列表對象) 第二個參數是autocompletetextview,第三個是嵌套的TextView,第四個是對List的引用,我只能假定最終應該是結果集,但顯然不是......這是驅使我瘋了,有沒有人有任何建議?我的主要問題是結果必須基於關鍵而不是價值,例如打字「a」可能意味着我想要在這裏做的「tiddlywinks」的結果 任何信息都會很好,非常感謝

回答

2

您首先爲編輯文本設置了textwatcher。像 urEditText.addTextChangedListener(searchInputTextWatcher);

private TextWatcher searchInputTextWatcher = new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence sequence, int start, int before, 
      int count) { 
     //Log.i("View adapter count",String.valueOf(locationViewAdapter.getCount())); 
     //if (locationViewAdapter.getCount()>1) 
      someAdapter.filterList(sequence); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
      int arg3) { 
    } 

    @Override 
    public void afterTextChanged(Editable arg0) { 
    } 
}; 

其中someAdapter是您的適配器,其中u實現方法filterList(在搜索字段中字型)

public void filterArrayList(CharSequence sequence) { 
    if (TextUtils.isEmpty(sequence)) { 
     someArrayList = (ArrayList<type>) cloneCategoryArrayList 
       .clone(); 

    } else { 
     someArrayList = (ArrayList<type>) cloneCategoryArrayList 
       .clone(); 
     if (!TextUtils.isEmpty(sequence)) { 

      List<type> tempCategoryArrayList = new ArrayList<type>(
        someArrayList); 
      for (type obj : tempCategoryArrayList) { 
       String typeName = obj.name; 

       if (!typename.toLowerCase().startsWith(
         sequence.toString().toLowerCase(), 0)) 
        somearrayList.remove(type); 
      } 
     } 
    } 
    notifyDataSetChanged(); 
} 
+0

我知道這是兩歲,但應該在函數調用filterList匹配someAdapter.filterList (序列); – Mike 2015-02-02 20:23:15