AutoCompleteTextView上的自定义ArrayAdapter未显示

时间:2015-08-03 12:25:43

标签: android

我搜索了SO和Google,但我找不到问题的答案。 我正在开发一个自定义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 个答案:

答案 0 :(得分:1)

好的,所以我解决了。 我只是将继承从ArrayAdapter更改为BaseAdapter,实现了必要的方法,现在正在运行。

由于