hendroprwk08

Untitled

Aug 24th, 2020 (edited)
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. class DessertAdapter extends RecyclerView.Adapter<DessertAdapter.GridViewHolder> {
  2.     private List<Dessert> desserts;
  3.     private Context context;
  4.  
  5.     public DessertAdapter(Context context, List<Dessert> desserts) {
  6.         this.desserts = desserts;
  7.         this.context = context;
  8.     }
  9.  
  10.     @NonNull
  11.     @Override
  12.     public GridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  13.         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
  14.         GridViewHolder viewHolder = new GridViewHolder(view);
  15.         return viewHolder;
  16.     }
  17.  
  18.     @Override
  19.     public void onBindViewHolder(@NonNull GridViewHolder holder, int position) {
  20.  
  21.         final String id = desserts.get(position).getIdMeal();
  22.         final String meal = desserts.get(position).getStrMeal();
  23.         final String photo = desserts.get(position).getStrMealThumb();
  24.  
  25.         holder.tvMeal.setText(meal);
  26.  
  27.         Glide.with(context)
  28.                 .load(photo)
  29.                 .into(holder.imgMeal);
  30.  
  31.         holder.itemView.setOnClickListener(new View.OnClickListener() {
  32.             @Override
  33.             public void onClick(View view) {
  34.                 Toast.makeText(context, meal, Toast.LENGTH_SHORT).show();
  35.             }
  36.         });
  37.     }
  38.  
  39.     @Override
  40.     public int getItemCount() {
  41.         return desserts.size();
  42.     }
  43.  
  44.     public class GridViewHolder extends RecyclerView.ViewHolder {
  45.         TextView tvMeal;
  46.         ImageView imgMeal;
  47.  
  48.         public GridViewHolder(@NonNull View itemView) {
  49.             super(itemView);
  50.  
  51.             tvMeal = (TextView) itemView.findViewById(R.id.tv_meal);
  52.             imgMeal = (ImageView) itemView.findViewById(R.id.img_meal);
  53.         }
  54.     }
  55. }
  56.  
Add Comment
Please, Sign In to add comment