6.17 8

七安。發表於2024-06-17
package com.zhen.accountbook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.zhen.accountbook.R;
import com.zhen.accountbook.db.AccountBean;

import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CancellationException;

public class AccountAdapter extends BaseAdapter {
    Context context;
    List<AccountBean> mDatas;
    LayoutInflater inflater;
    int year, month, day;

    public AccountAdapter(Context context, List<AccountBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        inflater = LayoutInflater.from(context);
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_mainlv, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        AccountBean accountBean = mDatas.get(i);
        viewHolder.typeIv.setImageResource(accountBean.getsImageId());
        viewHolder.typeTv.setText(accountBean.getTypename());
        viewHolder.beizhuTv.setText(accountBean.getBeizhu());
        viewHolder.moneyTv.setText("¥ " + accountBean.getMoney());
        if (accountBean.getYear() == year && accountBean.getMonth() == month && accountBean.getDay() == day) {
            String time = accountBean.getTime().split(" ")[1];
            viewHolder.timeTv.setText("今天 " + time);
        } else {
            viewHolder.timeTv.setText(accountBean.getTime());
        }
        return view;
    }

    class ViewHolder {
        ImageView typeIv;
        TextView typeTv, beizhuTv, timeTv, moneyTv;

        public ViewHolder(View view) {
            typeIv = view.findViewById(R.id.item_mainlv_iv);
            typeTv = view.findViewById(R.id.item_mainlv_tv_title);
            beizhuTv = view.findViewById(R.id.item_mainlv_tv_beizhu);
            timeTv = view.findViewById(R.id.item_mainlv_tv_time);
            moneyTv = view.findViewById(R.id.item_mainlv_tv_money);
        }
    }
}