package com.example.bpa.view.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.bpa.R;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_MATERIAL;
import com.example.bpa.helper.T;
import com.example.bpa.view.inteface.MyClickListener;

import java.util.List;

/**
 * 物料数据表格
 */
public class wl_adapter extends ArrayAdapter<BPA_MATERIAL> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private List<BPA_MATERIAL> datas;
    public wl_adapter(@NonNull Context context, int resource, @NonNull List<BPA_MATERIAL> objects,MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        datas=objects;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BPA_MATERIAL bpa_material = (BPA_MATERIAL) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.wl_item, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name = view.findViewById(R.id.name);
        Button button = view.findViewById(R.id.button_item);
        // 设置要显示的图片和文字
        name.setText(bpa_material.name);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_material);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"删除物料出错:"+e.getMessage());
                }
            }
        });
        return view;
    }
}