import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;

import java.util.List;

import club.tushar.customlistview.R;
import club.tushar.customlistview.databinding.RowCustomItemBinding;
import club.tushar.customlistview.models.MainModel;

public class ListviewAdapters extends BaseAdapter {

    private Context context;
    private List<MainModel> dtos;

    public ListviewAdapters(Context context, List<MainModel> dtos) {
        this.context = context;
        this.dtos = dtos;
    }

    @Override
    public int getCount() {
        return dtos.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder;

        if(convertView == null){
            holder = new Holder(context);
            convertView = holder.binding.getRoot();
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }

        holder.binding.tvItemName.setText(dtos.get(position).getName());
        holder.binding.tvItemNameSecondary.setText(dtos.get(position).getNameSecondary());

        Glide.with(context).load(dtos.get(position).getImageUrl())
                .into(holder.binding.ivImage);

        return convertView;
    }


    private class Holder{
        RowCustomItemBinding binding;
        Holder(Context context){
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.row_custom_item, null, true);
        }
    }
}