I'm trying to get suggestion list from firebase and display it in autocompletetextview as user types. When I use it with normal adapter ( ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
) it works fine. But I need custom adapter. And when use my custom adapter getView() is not being called, and I suppose therefore the dropdownlist is not being appeared.
I couldn't have figured out what is wrong. So here is my code:
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;
}
}
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)
{
}
});
you are returning wrong values in these overrided methods:
Change these methods:
@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
}
To this:
@Override
public int getCount() {
return suggestions.size();
}
@Override
public Object getItem(int index) {
return suggestions.get(index);
}