AutoCompleteTextView自定义适配器不会调用getView()

时间:2019-02-05 10:00:13

标签: android autocompletetextview

我正在尝试从Firebase获取建议列表,并在autocompletetextview中将其显示为用户类型。当我将其与普通适配器(ArrayAdapter<>(this, android.R.layout.simple_list_item_1);)配合使用时,它可以正常工作。但是我需要自定义适配器。而且在使用我的自定义适配器时,不会调用 getView(),因此我想因此不会显示下拉列表。

我无法弄清楚哪里出了问题。所以这是我的代码:

CustomAdapter:

public class KeywordSuggestionAdapter  extends ArrayAdapter
    {
        private static final String TAG = "KeywordSuggestAdapter";
        private List<KeywordSuggestion> suggestions;
  private  Context context;
private int itemLayout;
        public KeywordSuggestionAdapter( @NonNull Context context, int resource,  @NonNull List<KeywordSuggestion> objects)
            {
                super(context, resource, objects);
                Log.d(TAG, "KeywordSuggestionAdapter: init");
                this.context = context;
                this.suggestions = objects;
                this.itemLayout = resource;
            }



        static class ViewHolder
            {
                TextView keywordName;
                TextView keywordCount;
            }
        @Override
        public int getCount()
            {
                return 0;
            }

        @Override
        public Object getItem(int position)
            {
                return null;
            }

        @Override
        public long getItemId(int position)
            {
                return 0;
            }

        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;


            if (convertView == null)
                {
                    convertView = LayoutInflater.from(parent.getContext())
                            .inflate(itemLayout, parent, false);

                    holder = new ViewHolder();

                    holder.keywordName = (TextView) convertView.findViewById(R.id.tv_keyword_name);
                    holder.keywordCount = (TextView) convertView.findViewById(R.id.tv_keyword_count);

                    convertView.setTag(holder);
                }
            else
                {
                    holder = (ViewHolder) convertView.getTag();
                }

            KeywordSuggestion  suggestion = suggestions.get(position);

            if (suggestion != null) {
                    holder.keywordName.setText(suggestion.getKeyword_name());
                    holder.keywordCount.setText( context.getString(R.string.user_has_this_keyword,suggestion.getKeyword_user_count()));

            }
            return convertView;
        }
    }

活动

 suggestionAdapter = new KeywordSuggestionAdapter(mContext, R.layout.layout_keyword_recommendation, suggestions);
                actv_keyword.setAdapter(suggestionAdapter);
                actv_keyword.addTextChangedListener(new TextWatcher()
                    {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after)
                            {

                            }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count)
                            {
                                //If deleted a word
                                if(before >= count)
                                    {
                                suggestions.clear();
                                suggestionAdapter.clear();
                                        suggestionAdapter.notifyDataSetChanged();
                                    }


                                if (s.length() > 1)
                                    {


                                        final Query keywordsQuery = firebaseInstance.rootRef.child(mContext.getString(R.string.dbname_keyword_names))
                                                .startAt(s.toString())
                                                .endAt(s.toString() + '\uf8ff')
                                                .orderByChild(getString(R.string.field_keyword_name)).limitToFirst(5);

                                        keywordsQuery.addChildEventListener(new ChildEventListener()
                                            {
                                                @Override
                                                public void onChildAdded(@NonNull DataSnapshot snap, @Nullable String s)
                                                    {




                                                        KeywordSuggestion suggestion = new KeywordSuggestion(snap.child(getString(R.string.field_keyword_name)).getValue().toString(), ((long) snap.child(getString(R.string.field_keyword_user_count)).getValue()));
                                                        //Add the retrieved string to the list

                                                        suggestions.add(suggestion);
                                                        suggestionAdapter.add(suggestions);
                                                        suggestionAdapter.notifyDataSetChanged();



                                                        //     keywordsQuery.removeEventListener(this);

                                                    }


                                                @Override
                                                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
                                                    {

                                                    }

                                                @Override
                                                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot)
                                                    {

                                                    }

                                                @Override
                                                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
                                                    {

                                                    }

                                                @Override
                                                public void onCancelled(@NonNull DatabaseError databaseError)
                                                    {

                                                    }
                                            });

                                    }
                            }

                        @Override
                        public void afterTextChanged(Editable s)
                            {

                            }
                    });

2 个答案:

答案 0 :(得分:1)

您的getCount返回0

尝试将其替换为suggestions.size()

答案 1 :(得分:1)

您在这些替代方法中返回错误的值:

更改这些方法:

 @Override
    public int getCount()
        {
            return 0;  // you are supposed to return listview size not 0;
        }

    @Override
    public Object getItem(int position)
        {
            return null;  // for getting item position you need to pass your list here too
        }

对此:

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

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