mmayoub

MyAdapter.java

Mar 3rd, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | Software | 0 0
  1. package com.example.mylist;
  2.  
  3. import android.graphics.Color;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8.  
  9. import androidx.annotation.NonNull;
  10. import androidx.recyclerview.widget.RecyclerView;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
  15.     private final ArrayList<Item> data;
  16.  
  17.     public MyAdapter(ArrayList<Item> data) {
  18.         this.data = data;
  19.     }
  20.  
  21.     @NonNull
  22.     @Override
  23.     public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  24.         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_in_list_layout, parent, false);
  25.         MyViewHolder mvh = new MyViewHolder(view);
  26.         return mvh;
  27.     }
  28.  
  29.     @Override
  30.     public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  31.         Item currentItem = data.get(position);
  32.  
  33.         holder.tvName.setText(currentItem.getName());
  34.         holder.tvQuentity.setText(currentItem.getQuentity() + "");
  35.  
  36.         setColor(holder, position);
  37.  
  38.         holder.tvQuentity.setOnClickListener(new View.OnClickListener() {
  39.             @Override
  40.             public void onClick(View view) {
  41.                 currentItem.setQuentity(0);
  42.                 holder.tvQuentity.setText("0");
  43.  
  44.                 setColor(holder, position);
  45.             }
  46.         });
  47.     }
  48.  
  49.     private void setColor(MyViewHolder holder, int position) {
  50.         Item currentItem = data.get(position);
  51.         if (currentItem.getQuentity() < 20) {
  52.             holder.tvName.setBackgroundColor(Color.RED);
  53.             holder.tvQuentity.setBackgroundColor(Color.RED);
  54.         } else {
  55.             holder.tvName.setBackgroundColor(Color.GREEN);
  56.             holder.tvQuentity.setBackgroundColor(Color.GREEN);
  57.         }
  58.     }
  59.  
  60.     @Override
  61.     public int getItemCount() {
  62.         return data.size();
  63.     }
  64.  
  65.     public class MyViewHolder extends RecyclerView.ViewHolder {
  66.         private TextView tvName;
  67.         private TextView tvQuentity;
  68.  
  69.         public MyViewHolder(@NonNull View itemView) {
  70.             super(itemView);
  71.  
  72.             tvName = itemView.findViewById(R.id.tvName);
  73.             tvQuentity = itemView.findViewById(R.id.tvQuentity);
  74.         }
  75.     }
  76. }
  77.  
Add Comment
Please, Sign In to add comment