2015-08-03 132 views
0

我已經搜索過SO和Google,但找不到我的問題的答案。 我正在開發一個自定義的AutoCompleteTextView適配器(從ArrayAdapter繼承)並創建一個自定義的下拉視圖,但它永遠不會顯示出來。AutoCompleteTextView上的自定義ArrayAdapter未顯示

我試過到目前爲止:

MyAdapter.java:

public class FriendFilterAdapter extends ArrayAdapter<FriendFilterAdapter.FriendFilterResult> { 

    List<FriendFilterResult> list; 
    Context context; 
    FriendFilter friendFilter; 

    public FriendFilterAdapter(Context context) { 
     super(context, R.layout.adapter_friend_filter); 
     this.context = context; 
    } 

    @Override 
    public Filter getFilter() { 
     if(friendFilter == null) 
      friendFilter = new FriendFilter(); 

     return friendFilter; 
    } 

    public class FriendFilterResult { 
     private String name; 
     private Uri profileImageUri; 

     public FriendFilterResult(String name, Uri profileImageUri) { 
      this.name = name; 
      this.profileImageUri = profileImageUri; 
     } 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public Uri getProfileImageUri() { 
      return profileImageUri; 
     } 

     public void setProfileImageUri(Uri profileImageUri) { 
      this.profileImageUri = profileImageUri; 
     } 
    } 

    public static class FriendFilterViewHolder { 
     @Bind(R.id.friendFilterProfile) 
     public SimpleDraweeView profile; 
     @Bind(R.id.friendFilterName) 
     public TextView name; 

     public FriendFilterViewHolder(View v) { 
      ButterKnife.bind(this,v); 
     } 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return setupAdapterView(position,convertView,parent); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     return setupAdapterView(position, convertView, parent); 
    } 

    private View setupAdapterView(int position, View convertView, ViewGroup parent){ 
     FriendFilterResult result = list.get(position); 

     FriendFilterViewHolder viewHolder; 

     Log.d("custom adapter",result.getName()); 

     if(convertView != null) { 
      viewHolder = (FriendFilterViewHolder) convertView.getTag(); 
     }else{ 
      convertView = LayoutInflater.from(context).inflate(R.layout.adapter_friend_filter, parent,false); 
      viewHolder = new FriendFilterViewHolder(convertView); 
      convertView.setTag(viewHolder); 
     } 

     viewHolder.name.setText(result.getName()); 
     viewHolder.profile.setImageURI(result.getProfileImageUri()); 

     return convertView; 
    } 

    public class FriendFilter extends Filter{ 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 
      List<FriendFilterResult> friendFilterResults; 

      if(constraint != null && constraint.length() > 0){ 
       friendFilterResults = autocomplete(constraint.toString()); 
      }else{ 
       friendFilterResults = new ArrayList<>(); 
      } 

      results.count = friendFilterResults == null ? 0 : friendFilterResults.size(); 
      results.values = friendFilterResults; 

      return results; 
     } 

     private List<FriendFilterResult> autocomplete(String input){ 
      List<User> users; 
      List<FriendFilterResult> friendFilterResults; 

      ParseQuery<User> query = new ParseQuery<>(User.class); 
      query.whereContains("firstName",input); 
      query.selectKeys(Arrays.asList("firstName","profileImg")); 
      try { 
       users = query.find(); 
       friendFilterResults = new ArrayList<>(users.size()); 
      } catch (ParseException e) { 
       return null; 
      } 

      for(int i = 0; i < users.size();i++){ 
       User user = users.get(i); 
       friendFilterResults.add(new FriendFilterResult(user.getFullName(), Uri.parse(user.getProfileImg().getUrl()))); 
      } 

      return friendFilterResults; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 

      list = (List<FriendFilterResult>) results.values; 
      Log.d("custom adapter",list.size() + ""); 

      if(results.count > 0) 
       notifyDataSetChanged(); 
      else 
       notifyDataSetInvalidated(); 
     } 
    } 
} 

而且我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight"> 

    <com.facebook.drawee.view.SimpleDraweeView 
     android:id="@+id/friendFilterProfile" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="16dp" 
     app:roundAsCircle="true" /> 

    <TextView 
     android:id="@+id/friendFilterName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/friendFilterProfile" 
     android:ellipsize="end" 
     android:layout_marginRight="16dp" 
     android:layout_marginEnd="16dp" 
     android:layout_marginTop="16dp"/> 

</RelativeLayout> 

日誌告訴我很多的項目是如何被刊登在publishResults但下拉菜單從不顯示。

我是否錯過了某些東西,或者我的代碼有問題嗎?

謝謝!

回答

1

好吧,所以我解決了它。 我只是將繼承從ArrayAdapter更改爲BaseAdapter,實施了必要的方法,現在正在開展工作。

謝謝

+0

這不是問題,我使用ArrayAdapter,它工作。 – SpyZip

相關問題