How to set Image byte array to Recyclerview in Android? code example
Example 1: how to convert bytes to bitmap and show in recyclerview
@Override public int getItemCount() { return mImageNames.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ CircleImageView image; TextView imageName; RelativeLayout parentLayout; public ViewHolder(View itemView) { super(itemView); image = itemView.findViewById(R.id.image); imageName = itemView.findViewById(R.id.image_name); parentLayout = itemView.findViewById(R.id.parent_layout); } }
Example 2: how to convert bytes to bitmap and show in recyclerview
public class RecyclerViewAdapter extends RecyclerView.Adapter{ private static final String TAG = "RecyclerViewAdapter"; private ArrayList mImageNames = new ArrayList<>(); private ArrayList mImages = new ArrayList<>(); private List mUploads; private Context mContext; public RecyclerViewAdapter(Context context, ArrayList imageNames, ArrayList images ) { mImageNames = imageNames; mImages = images; mContext = context; } public RecyclerViewAdapter(Context context, List uploads ) { mUploads = uploads; mContext = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { Log.d(TAG, "onBindViewHolder: called."); Glide.with(mContext) .asBitmap() .load(mImages.get(position)) .into(holder.image); holder.imageName.setText(mImageNames.get(position)); holder.parentLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d(TAG, "onClick: clicked on: " + mImageNames.get(position)); System.out.println("RE"); Toast.makeText(mContext, mImageNames.get(position), Toast.LENGTH_SHORT).show(); System.out.println("RE1"); Intent intent = new Intent(mContext, GalleryActivity.class); intent.putExtra("image_url", mImages.get(position)); intent.putExtra("image_name", mImageNames.get(position)); System.out.println("RE2"); mContext.startActivity(intent); System.out.println("RE2"); } }); }``` Below is the View Holder.