Adapter

ItemAdapter.java

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
    private final ArrayList<Item> listItem = new ArrayList<Item>();
    private Context context;

    public ItemAdapter(Context context) {
        this.context = context;
    }

    public void setListItem(ArrayList<Item> listItem) {

        if (listItem.size() > 0) {
            this.listItem.clear();

        }

        this.listItem.addAll(listItem);
        notifyDataSetChanged();

    }

    public void addItem(Item note) {
        this.listItem.add(note);
        notifyItemInserted(listItem.size() - 1);
    }

    public void updateItem(int position, Item item) {
        this.listItem.set(position, item);
        notifyItemChanged(position, item);
    }

    public void removeItem(int position) {
        this.listItem.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, listItem.size());
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item, parent, false);
        return new ItemViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolder holder, final int position) {
        holder.tvItemName.setText(listItem.get(position).getName());
        holder.tvItemBrand.setText(listItem.get(position).getBrand());
        holder.tvItemPrice.setText("" + listItem.get(position).getPrice());

        holder.cvItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Name : " + listItem.get(position).getName(), Toast.LENGTH_LONG).show();
                Intent intent = new Intent(context, AddItemActivity.class);
                intent.putExtra("position",position);
                intent.putExtra("item",listItem.get(position));
                context.startActivity(intent);


            }
        });

        holder.cvItem.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return true;
            }
        });

    }


    @Override
    public int getItemCount() {
        return listItem.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemName, tvItemBrand, tvItemPrice;

        CardView cvItem;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);

            tvItemName = itemView.findViewById(R.id.tv_item_name);
            tvItemBrand = itemView.findViewById(R.id.tv_item_brand);
            tvItemPrice = itemView.findViewById(R.id.tv_item_price);
            cvItem = itemView.findViewById(R.id.cv_item);
        }
    }
}

item_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/cv_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp"
        app:cardMaxElevation="4dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/tv_item_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:gravity="start"
                android:text="Tanggal"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/tv_item_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:gravity="start"
                android:text="Name"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_item_brand"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:gravity="start"

                android:text="Brand"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/tv_item_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:text="Price"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="14sp" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

Last updated

Was this helpful?