I have a condition where auto suggest need to be implemented. For that purpose, I have been trying to use the custom RecyclerView.Adapter.But the problem is,AutoCompleteTextView seems to be made for BaseAdapter and its child class.
Therefore, the adapter is not accepted. Is there other way, I could use RecyclerView adapter with AutoCompleteTextView. Is the ArrayListAdapter or the BaseAdapter only solution for AutCompleteTextView?
addHeaderView() but you can achieve this by adding a type to your adapter for header. everything seems to be OK and it should work, but however make the recycler view MATCH_PARENT see if anything changes. also if its possible make the recycler view the root of that layout (its good for performance).
Unlike RecyclerView. Adapter implementation, we don't need to override getItemCount() method as ListAdapter manages the list for us. So we only need to implement two methods onCreateViewHodler() and onBindViewHolder() . All we have to do now is submit new list whenever we have it.
AutoCompleteTextView
requires a ListAdapter
that is also Filterable
. You can implement this by having a wrapper class around a RecyclerView.Adapter
that extends BaseAdapter
and delegates calls to the underlying RecyclerView.Adapter
. Here's an example:
public class RecyclerBaseAdapter<VH extends RecyclerView.ViewHolder>
extends BaseAdapter implements Filterable {
private final RecyclerView.Adapter<VH> mAdapter;
public RecyclerBaseAdapter(RecyclerView.Adapter<VH> adapter) {
mAdapter = adapter;
}
@Override
public int getItemViewType(int position) {
return mAdapter.getItemViewType(position);
}
@Override
public int getCount() {
return mAdapter.getItemCount();
}
@Override
public Object getItem(int position) {
// not supported
return null;
}
@Override
public long getItemId(int position) {
return mAdapter.getItemId(position);
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
VH holder;
if (convertView == null) {
holder = mAdapter.createViewHolder(parent, getItemViewType(position));
convertView = holder.itemView;
convertView.setTag(holder);
} else {
holder = (VH) convertView.getTag();
}
mAdapter.bindViewHolder(holder, position);
return holder.itemView;
}
@Override
public Filter getFilter() {
// TODO: return a real filter
return null;
}
}
Now you can do mAutoCompleteTextView.setAdapter(new RecyclerBaseAdapter(mRecyclerViewAdapter))
. I only tested this on a Spinner
, but it should also work for your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With