Maruf_Hasan

recyclerviewadapter

Apr 4th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package com.example.recyclerview;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9.  
  10. import androidx.annotation.NonNull;
  11. import androidx.recyclerview.widget.RecyclerView;
  12.  
  13. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
  14. String data1[],data2[];
  15. int img[];
  16. Context ctx;
  17. public MyAdapter(Context ct,String s1[],String s2[],int arr[])
  18. {
  19. data1=s1;
  20. data2=s2;
  21. ctx=ct;
  22. img=arr;
  23. }
  24. @NonNull
  25. @Override
  26. public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  27. LayoutInflater myinflator=LayoutInflater.from(ctx);
  28. View MyView=myinflator.inflate(R.layout.my_row,parent,false);
  29. return new MyHolder(MyView);
  30. }
  31.  
  32. @Override
  33. public void onBindViewHolder(@NonNull MyHolder holder, int position) {
  34. holder.t1.setText(data1[position]);
  35. holder.t2.setText(data2[position]);
  36. holder.myimage.setImageResource(img[position]);
  37. }
  38.  
  39. @Override
  40. public int getItemCount() {
  41.  
  42. return data1.length;
  43. }
  44.  
  45. public class MyHolder extends RecyclerView.ViewHolder {
  46. TextView t1,t2;
  47. ImageView myimage;
  48. public MyHolder(@NonNull View itemView) {
  49. super(itemView);
  50. t1=(TextView)itemView.findViewById(R.id.textView);
  51. t2=(TextView)itemView.findViewById(R.id.textView2);
  52. myimage=(ImageView)itemView.findViewById(R.id.imageView);
  53. }
  54. }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment