Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mylist;
- import android.graphics.Color;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.RecyclerView;
- import java.util.ArrayList;
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
- private final ArrayList<Item> data;
- public MyAdapter(ArrayList<Item> data) {
- this.data = data;
- }
- @NonNull
- @Override
- public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_in_list_layout, parent, false);
- MyViewHolder mvh = new MyViewHolder(view);
- return mvh;
- }
- @Override
- public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
- Item currentItem = data.get(position);
- holder.tvName.setText(currentItem.getName());
- holder.tvQuentity.setText(currentItem.getQuentity() + "");
- setColor(holder, position);
- holder.tvQuentity.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- currentItem.setQuentity(0);
- holder.tvQuentity.setText("0");
- setColor(holder, position);
- }
- });
- }
- private void setColor(MyViewHolder holder, int position) {
- Item currentItem = data.get(position);
- if (currentItem.getQuentity() < 20) {
- holder.tvName.setBackgroundColor(Color.RED);
- holder.tvQuentity.setBackgroundColor(Color.RED);
- } else {
- holder.tvName.setBackgroundColor(Color.GREEN);
- holder.tvQuentity.setBackgroundColor(Color.GREEN);
- }
- }
- @Override
- public int getItemCount() {
- return data.size();
- }
- public class MyViewHolder extends RecyclerView.ViewHolder {
- private TextView tvName;
- private TextView tvQuentity;
- public MyViewHolder(@NonNull View itemView) {
- super(itemView);
- tvName = itemView.findViewById(R.id.tvName);
- tvQuentity = itemView.findViewById(R.id.tvQuentity);
- }
- }
- }
Add Comment
Please, Sign In to add comment