How to create a custom adapter for a RecyclerView?

J.erome :

I followed the official Android tutorialand this tutorial but they are really different and I can't figure out how to create my custom adapter.

I have a RecyclerView where I want to display some musics, so in my custom adapter I did:

   public class SongPlayListAdapter extends RecyclerView.Adapter<SongPlayListAdapter.MyViewHolder> {
    private ArrayList<String> songList;
    private Context context;
    private int viewRes;
    private Resources res;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {  
        /* here it's wrong but I don't know what to do 
        R.id.title_song_playlist TextView mTitleSongPlaylistTextView;
        R.id.group_playlist TextView mGroupSongPlaylistTextView;
        R.id.preview_song_playlist ImageView mPreviewSongPlaylistImageView; */

        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public SongPlayListAdapter(Context context, int viewResourceId,
                               ArrayList<String> songList) {
        this.songList = songList;
        this.context = context;
        this.viewRes = viewResourceId;
        this.res = context.getResources();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // create a new view
        TextView title_song = (TextView);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }
}

Problem is I don't know how to populate my RecyclerView neither what should be in the overrided methods. I did it with a normal list and my adapter was like this :

          View view = convertView;
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(viewRes, parent, false);
        }
        final String song= songList.get(position);
        if (song != null) {
            final TextView title = (TextView) view.findViewById(R.id.title_song);
            final TextView group = (TextView) view.findViewById(R.id.group_song);
            final ImageView image= (ImageView) view.findViewById(R.id.image_song);
            title.setText(song);
            description.setText(song);
        }
        return view;
    }

So how can I manage to fix my adapter ? I'm really lost and the tutorial isn't really helping

Ravi :

You can use the holder something like this , where in bind data you will set the data for each row item. Can follow the given example below

public class RestaurantListAdapter extends RecyclerView.Adapter<RestaurantListAdapter.RestaurantViewHolder> {
    private ArrayList<Restaurant> mRestaurants = new ArrayList<>();
    private Context mContext;

    public RestaurantListAdapter(Context context, ArrayList<Restaurant> restaurants) {
        mContext = context;
        mRestaurants = restaurants;
    }

    public class RestaurantViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.restaurantImageView) ImageView mRestaurantImageView;
        @Bind(R.id.restaurantNameTextView) TextView mNameTextView;
        @Bind(R.id.categoryTextView) TextView mCategoryTextView;
        @Bind(R.id.ratingTextView) TextView mRatingTextView;

        private Context mContext;

        public RestaurantViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mContext = itemView.getContext();
        }

        public void bindRestaurant(Restaurant restaurant) {
            mNameTextView.setText(restaurant.getName());
            mCategoryTextView.setText(restaurant.getCategories().get(0));
            mRatingTextView.setText("Rating: " + restaurant.getRating() + "/5");
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220130&siteId=1