package jp.gihyo.mitoma.android.s1602listview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;

public class FriendsAdapter extends ArrayAdapter<FriendsDTO>{
    private Context context;
    private int customLayout;
    private ArrayList<FriendsDTO> friendList;
    private ArrayList<Integer> imageList;
    public FriendsAdapter(Context context,int customLayout,
                          ArrayList<FriendsDTO> friendList,ArrayList<Integer> imageList){
        super(context,customLayout,friendList);
        this.context = context;
        this.customLayout = customLayout;
        this.friendList = friendList;
        this.imageList = imageList;
    }
    @NonNull
    @Override
    public View getView(int position, View shareView, @NonNull ViewGroup parent) {
        // Viewの再利用（パフォーマンス向上）
        if (shareView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            shareView = inflater.inflate(customLayout, parent, false);
        }
        // データを取得
        FriendsDTO dto = friendList.get(position);
        // 行Viewからウィジェットの参照変数を取得してデータをセット
        TextView tvID = shareView.findViewById(R.id.tvID);
        TextView tvName = shareView.findViewById(R.id.tvName);
        TextView tvPhone = shareView.findViewById(R.id.tvPhone);
        ImageView ivFriend = shareView.findViewById(R.id.friendImage);

        tvID.setText(dto.id);
        tvName.setText(dto.name);
        tvPhone.setText(dto.phone);
        shareView.setTag(dto.id);
        ivFriend.setImageResource(imageList.get(position));
        //戻り(データ格納済みの行View)
        return shareView;
    }
}
