Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AutoCompleteTextView with RecyclerView.Adapter but not with ArrayAdapter in android

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?

like image 580
Ait Bri Avatar asked Nov 14 '15 17:11

Ait Bri


People also ask

Is there an addHeaderView equivalent for RecyclerView?

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).

What are differences between RecyclerView adapter and ListAdapter?

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.


1 Answers

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.

like image 57
chessdork Avatar answered Nov 08 '22 17:11

chessdork