1package com.jbws.myviewbindingdemo.adapter;
2
3import android.view.LayoutInflater;
4import android.view.View;
5import android.view.ViewGroup;
6
7import androidx.annotation.NonNull;
8import androidx.recyclerview.widget.RecyclerView;
9
10import com.jbws.myviewbindingdemo.databinding.RowXmlViewBinding;
11import com.jbws.myviewbindingdemo.pojo.ModelObject;
12
13import java.util.ArrayList;
14
15public class RecyclerViewListAdapter extends RecyclerView.Adapter<RecyclerViewListAdapter.ViewHolder> {
16 public ArrayList<ModelObject> modelObjectArrayList;
17
18 public RecyclerViewListAdapter(ArrayList<ModelObject> modelObjectArrayList) {
19 this.modelObjectArrayList = modelObjectArrayList;
20 }
21
22 @NonNull
23 @Override
24 public RecyclerViewListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
25 return new ViewHolder(RowXmlViewBinding.inflate(LayoutInflater.from(parent.getContext()),
26 parent, false));
27 }
28
29 @Override
30 public void onBindViewHolder(@NonNull RecyclerViewListAdapter.ViewHolder holder, final int position) {
31 ModelObject modelObject = modelObjectArrayList.get(position);
32 holder.rowXmlViewBinding.txtObjectName.setText(modelObject.getFullName());
33 holder.rowXmlViewBinding.btnUpdateName.setOnClickListener(view -> {
34 Log.i("LOG_TAG", "Full Name: " + modelObject.getFullName);
35 });
36 }
37
38 @Override
39 public int getItemCount() {
40 return modelObjectArrayList == null ? 0 :
41 modelObjectArrayList.size();
42 }
43
44 public static class ViewHolder extends RecyclerView.ViewHolder {
45 private RowXmlViewBinding rowXmlViewBinding;
46
47 public ViewHolder(RowXmlViewBinding rowXmlViewBinding) {
48 super(rowXmlViewBinding.getRoot());
49 this.rowXmlViewBinding = rowXmlViewBinding;
50 }
51 }
52}
53