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

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.res.ResSubOrder;
import com.bonait.bnframework.common.db.res.SuOrderTJ;

import java.util.List;


public class ddtj_adapter extends ArrayAdapter<SuOrderTJ> {
    private List<SuOrderTJ> datas;
    private Context context;

    private int resource;

    public ddtj_adapter(@NonNull Context context, int resource, @NonNull List<SuOrderTJ> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource=resource;
        datas=objects;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        SuOrderTJ order = (SuOrderTJ) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(this.resource, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name,ddzl,ddzl_yzz,ddzl_yc;
        name = (TextView) view.findViewById(R.id.name);
        ddzl = (TextView) view.findViewById(R.id.ddzl);
        ddzl_yzz = (TextView) view.findViewById(R.id.ddzl_yzz);
        ddzl_yc = (TextView) view.findViewById(R.id.ddzl_yc);
        name.setText(order.name+"");
        ddzl.setText(order.Count+"");
        ddzl_yzz.setText(order.Yzz_Count+"");
        ddzl_yc.setText(order.Yc_Count+"");
        return view;
    }
    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }
    /**
     * 刷新
     * @param
     */
    public void refresh(){
        Activity activity= findActivity(context);
        if(activity!=null)
        {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        notifyDataSetChanged();
                    } catch (Exception e) {

                    }
                }
            });
        }
    }
}
