14

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?

Ait Bri
  • 498
  • 6
  • 15
  • 1
    Hey Amit, Have you resolve this issue? – UrMi May 13 '16 at 07:24
  • No @UrMi. Hope this might help you. [link 1](https://github.com/Malinskiy/SuperRecyclerView) [link2](https://github.com/cymcsg/UltimateRecyclerView) – Ait Bri May 17 '16 at 05:16

1 Answers1

11

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.

Bouh
  • 1,303
  • 2
  • 10
  • 24
chessdork
  • 1,999
  • 1
  • 19
  • 20
  • Hi @chessdork, I ll definitely try it. Thank you. – Ait Bri Mar 09 '17 at 18:06
  • 2
    Using this code verbatim, I can only get the constructor to execute, none of the other methods get entered and nothing displays in my autocompletetextview – CQM Mar 14 '17 at 20:56
  • This approach did work for me. I also had to implement getViewTypeCount() because the RecyclerView.Adapter I wrapped used multiple item view types. – leorleor May 17 '17 at 21:04
  • @CQM and ssPerman01 : You will need to create a Filter according to your need and implement it. – Nirab Pudasaini Jul 10 '18 at 08:14
  • can share the filter code because I am getting error while try to retune filter? – Farmer Dec 20 '22 at 11:29