2014-09-26 155 views
1

我正在處理自動完成文本視圖用於獲取匹配地方的需求。用戶必須輸入幾個字符,並從服務器返回與這些字符匹配的Localities列表。自動完成文本視圖然後應該顯示這個列表作爲下拉(或彈出)。出於某種原因,即使我從服務器接收到有效的地區信息,自動完成下拉菜單也不會出現。這是我的代碼。AutoCompleteTextView不顯示任何下拉菜單

局部性(型號)

public class Locality { 
    private int Id; 
    private String name; 
    private boolean selected; 

    //Getters and Setters 
} 

DonorSearchFragment.xml(含有AutoCompleteTextView佈局文件)

<AutoCompleteTextView 
    android:id="@+id/actLocality" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

LocalityListAdapter視圖(locality_row.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    ... 
    <TextView 
     android:id="@+id/txvLocalityName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

LocalityListAdapter.java

public class LocalityListAdapter extends ArrayAdapter<Locality> { 

    List<Locality> localities; 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //The Current View 
     View view = convertView; 
     if (view == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.locality_row, null); 
     } 
     Locality currentLocality = localities.get(position); 
     if (currentLocality != null) { 
      TextView txvLocalityName = (TextView) view.findViewById(R.id.txvLocalityName); 
      txvLocalityName.setText(currentLocality.getName()); 
     } 
     return view; 
    } 
} 

DonorSearchFragment.java(片段表示AutoCompleteTextView)

onPostExecute()我檢查,我接收有效地點列表,然後將它們推入支持適配器的LocalityList中,但這些地區不會通過下拉列表顯示。實際上沒有顯示下拉菜單。任何可能導致錯誤或遺漏的原因?

回答

1

我覺得有一個更好的方法來處理這種情況,避免使用AsyncTask。可以使用Filter,它提供了performFiltering()實際在工作線程中運行的回調。這裏匹配的數據可以被決定/準備(如通過服務器調用或類似的)。在準備結果時,可以使用在UI線程上運行的publishResults回調來修改支持自定義適配器的列表。我剛剛開始瞭解Android適配器,所以我可能在這裏不正確。下面是我使用

public class LocalityListAdapter extends ArrayAdapter<Locality> { 

    List<Locality> localities; 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (view == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.locality_row, null); 
     } 
     Locality currentLocality = localities.get(position); 
     if (currentLocality != null) { 
      TextView txvLocalityName = (TextView) view.findViewById(R.id.txvLocalityName); 
      txvLocalityName.setText(currentLocality.getName()); 
     } 
     return view; 
    } 

    @Override 
    public Filter getFilter() { 
     return localityFilter; 
    } 

    Filter localityFilter = new Filter() { 
     @Override 
     public CharSequence convertResultToString(Object resultValue) { 
      Locality locality = (Locality) resultValue; 
      return locality.getName(); 
     } 

     protected FilterResults performFiltering(CharSequence constraint) { 
      List<Locality> localityList = new ArrayList<Locality>(); 
      ...Get Data from Server 
      //Return FilterResults created from response 
      FilterResults filterResults = new FilterResults(); 
      filterResults.values = localityList; 
      filterResults.count = localityList.size(); 
      return filterResults; 
     } 

     @Override 
     protected void publishResults(CharSequence charSequence, FilterResults filterResults) { 
      localities.clear(); 
      List<Locality> filteredList = (List<Locality>) filterResults.values; 
      for(Locality locality : filteredList) { 
       localities.add(locality); 
      } 
      notifyDataSetChanged(); 
     } 
    } 
}    

DonorSearchFragment.java(從MainActivity)

public class DonorSearchFragment extends Fragment { 
    ... 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     searchFragmentView = inflater.inflate(R.layout.fragment_donor_search, container, false); 

     //Locality AutoComplete 
     localityList = new ArrayList<Locality>(); 
     actLocality = (AutoCompleteTextView) searchFragmentView.findViewById(R.id.actLocality); 
     localityListAdapter = new LocalityListAdapter(this.getActivity(), R.layout.locality_row, localityList); 
     actLocality.setAdapter(localityListAdapter); 
     ... 
     return searchFragmentView; 
    } 
} 
代碼