Search code examples
javaandroidandroid-recyclerviewbaseadapter

How to Set BaseAdapter for RecyclerView?


Whenever I need to implement a custom ListView or GridView . I always use a BaseAdapter class like this:

public class CustomFlowerGridViewAdapter extends BaseAdapter {

private HashMap<String, String> mFlowerFeed = new HashMap<>();

private Context mContext;
static LayoutInflater mFlowerInflater = null;

CustomFlowerGridViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {

    super();
    this.mContext = context;
    MainActivity.flowerArrayFeedList = data;
    mFlowerInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return MainActivity.flowerArrayFeedList.size();
}

@Override
public Object getItem(int i) {
    return MainActivity.flowerArrayFeedList.size();
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    final ViewHolder holder;

    if(view == null){

        view = mFlowerInflater.inflate(R.layout.flower_grid_row, viewGroup, false);

        holder = new ViewHolder();

        holder.name = (TextView) view.findViewById(R.id.mainGridName);

        view.setTag(holder);
        mFlowerFeed = MainActivity.flowerArrayFeedList.get(i);
        mFlowerImage = MainActivity.flowerArrayImageList.get(i);

    }
    else{
        holder = (ViewHolder) view.getTag();
        mFlowerFeed = MainActivity.flowerArrayFeedList.get(i);
        mFlowerImage = MainActivity.flowerArrayImageList.get(i);

    }

    holder.name.setText(mFlowerFeed.get("name"));

    return view;
}

private class ViewHolder {

    TextView name

 }
}

However, now I'm trying to implement a Horizontal ListView. After doing some research, I found that the "correct" way to implement a Horizontal ListView is to use a RecyclerView. This is what I have in my MainActivity

CustomFlowerGridViewAdapter customFlowerGridViewAdapter = new CustomFlowerGridViewAdapter(MainActivity.this, flowerArrayFeedList);
    flowersGrid.setAdapter(customFlowerGridViewAdapter);

The problem is when I try set the adapter.

enter image description here

How do I set a BaseAdapter to a RecyclerView?


Solution

  • You don't. RecylerViews accept another kind of adapter, different from the one in ListView. A simple example of adapter with a list of strings:

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTextView;
            public ViewHolder(TextView v) {
                super(v);
                mTextView = v;
            }
        }
    
       public MyAdapter(String[] myDataset) {
            mDataset = myDataset;
        }
    
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            TextView v = (TextView) LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.my_text_view, parent, false);
            ...
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }
    
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.mTextView.setText(mDataset[position]);
    
        }
    
    
        @Override
        public int getItemCount() {
            return mDataset.length;
        }
    }
    

    This one can be found in the docs. So you need to implement a ViewHolder class that and your adapter will use. Your adapter needs to extend RecyclerView.Adapter.

    To set the list to be horizontal you need to set a LinearLayourManager in your recycler view with the Horizontal property set.

    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    MyAdapter adapter = new MyAdapter(dataset);
    recyclerView.setAdapter(adapter);