AutoCompleteTextView 自定义适配器不调用 getView()
AutoCompleteTextView Custom Adapter doesn't call getView()
我正在尝试从 firebase 获取建议列表并将其作为用户类型显示在 autocompletetextview 中。当我将它与普通适配器 (ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
) 一起使用时,它工作正常。但我需要自定义适配器。当使用我的自定义适配器时 getView() 没有被调用 ,因此我想下拉列表没有出现。
我不知道出了什么问题。所以这是我的代码:
自定义适配器:
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;
}
}
Activity:
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)
{
}
});
你的getCount
return0
尝试将其替换为suggestions.size()
您在这些重写的方法中返回了错误的值:
更改这些方法:
@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);
}
我正在尝试从 firebase 获取建议列表并将其作为用户类型显示在 autocompletetextview 中。当我将它与普通适配器 (ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
) 一起使用时,它工作正常。但我需要自定义适配器。当使用我的自定义适配器时 getView() 没有被调用 ,因此我想下拉列表没有出现。
我不知道出了什么问题。所以这是我的代码:
自定义适配器:
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;
}
}
Activity:
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)
{
}
});
你的getCount
return0
尝试将其替换为suggestions.size()
您在这些重写的方法中返回了错误的值:
更改这些方法:
@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);
}