package com.bonait.bnframework.modules.home.adapter;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_PROCESS;
import com.bonait.bnframework.common.db.mode.BPA_PROCESSModel;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.helper.I.MyClickListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 工序表格
 */
public class process_adapter extends ArrayAdapter<BPA_PROCESS> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private List<BPA_PROCESS> datas;
    int resource1;
    public process_adapter(@NonNull Context context, int resource, @NonNull List<BPA_PROCESS> objects, MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        datas=objects;
        this.resource1=resource;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BPA_PROCESS lc = (BPA_PROCESS) getItem(position);//得到当前项选中item实例

        ArrayList<BPA_PROCESSModel> chid=QueryDB.GetProcessModelProcessID(lc.id);
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(resource1, parent, false);
        //分别获取 image view 和 textview 的实例
        ListView list=view.findViewById(R.id.datatab);
        processchid_adapter adapter = new processchid_adapter(getContext(), R.layout.processchid_item,chid);
        list.setAdapter(adapter);
        TextView xh = view.findViewById(R.id.xh);
        TextView name = view.findViewById(R.id.name);


        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, list);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
       // totalHeight+=30;


        ViewGroup.LayoutParams params=view.getLayoutParams();
        if(list.getCount()>0)
        {
            params.height = totalHeight+(list.getDividerHeight() * (list.getCount() - 1))+50;
        }else
        {
            params.height = totalHeight+50;
        }

        view.setLayoutParams(params);
        // 设置要显示的图片和文字
        xh.setText(lc.sort+"");
        name.setText(lc.name);
        return view;
    }
}