package com.bpa.scalage.control;

import android.content.Context;
import android.content.Intent;
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.bpa.scalage.MainActivity;
import com.bpa.scalage.R;
import com.bpa.scalage.data.DataBus;
import com.bpa.scalage.helper.T;
import com.bpa.scalage.mode.Foods.FoodModel;
import com.bpa.scalage.mode.Order.BPA_SubOrder;

import java.util.List;

/**
 * 以消费列表
 */
public class ConsumedAdapter extends ArrayAdapter<BPA_SubOrder> {

    public ConsumedAdapter(@NonNull Context context, int resource, @NonNull List<BPA_SubOrder> objects) {
        super(context, resource, objects);
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BPA_SubOrder order=(BPA_SubOrder)getItem(position);//得到当前项的 Fruit 实例
        //为每一个子项加载设定的布局
        View view= LayoutInflater.from(getContext()).inflate(R.layout.consumed_item,parent,false);
        //分别获取 image view 和 textview 的实例
        TextView name =view.findViewById(R.id.tv_consumed_name);
        TextView weight =view.findViewById(R.id.tv_consumed_weight);
        TextView price =view.findViewById(R.id.tv_consumed_price);
        TextView dj =view.findViewById(R.id.tv_consumed_dj);

        // 设置要显示的图片和文字
        name.setText(order.goodName);
        weight.setText(order.consumeUnit+"g");

        try{
            // 核心代码，如果读取数据库内容
            double bz=0;
            if(!order.remark.isEmpty() && order.remark!="")
            {
                bz=Double.parseDouble(order.remark);
            }
            dj.setText(String.format("%.2f", bz)+" ¥/50g");
            price.setText("¥"+String.format("%.2f", order.goodsRealPrice));
        }catch(Exception e){
            //异常捕获，例如输出异常信息
            dj.setText(String.format("%.2f", 0)+" ¥/50g");
            price.setText("¥"+String.format("%.2f", 0));
        }finally{
            //善后处理，无论是否异常，都会执行的部分
        }
        return view;
    }

}
