package net.ddns.mahaljsp.ch13_01_sqlite;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Thomas Wu on 2016/12/5.
 */

public class ItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    List<Item>list;
    public ItemAdapter(Context context, List<Item>list){
        this.mInflater = LayoutInflater.from(context);
        this.list=list;
    }
    public void setList(List<Item> list){
        this.list=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            holder=new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_view, null);
            holder.txtId = (TextView)convertView.findViewById(R.id.txtId);
            holder.txtFirstName = (TextView)convertView.findViewById(R.id.txtFirstName);
            holder.txtLastName = (TextView)convertView.findViewById(R.id.txtLastName);
            holder.txtTel=(TextView)convertView.findViewById(R.id.txtTel);
            holder.txtEmail=(TextView)convertView.findViewById(R.id.txtEmail);
            holder.txtBirthday=(TextView)convertView.findViewById(R.id.txtBirthday);
            holder.txtAddress=(TextView)convertView.findViewById(R.id.txtAddress);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.txtId.setText(String.valueOf(list.get(position).getId()));
        holder.txtFirstName.setText(list.get(position).getFirstname());
        holder.txtLastName.setText(list.get(position).getLastname());
        holder.txtTel.setText(list.get(position).getTel());
        holder.txtEmail.setText(list.get(position).getEmail());
        Date d=new Date(list.get(position).getBirthday());
        holder.txtBirthday.setText(DateFormat.getDateInstance(DateFormat.SHORT, Locale.TAIWAN).format(d));
        holder.txtAddress.setText(list.get(position).getAddress());
        if(position%2==0)
            convertView.setBackgroundColor(Color.rgb(0xaa,0xaa,0xaa));
        else
            convertView.setBackgroundColor(Color.rgb(0xcc, 0xcc, 0xcc));
        return convertView;
    }
    public final class ViewHolder {
        public TextView txtId, txtFirstName, txtLastName, txtTel, txtEmail, txtAddress, txtBirthday;
    }
}
