package com.android.chmo.ui.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

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

/**
 * Adapter基类
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected LayoutInflater mInflater;
    protected List<T> mList;

    public CommonAdapter() {
        mList = new ArrayList<T>();
    }

    public CommonAdapter(Context context) {
        super();
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public CommonAdapter(Context context, List<T> list) {
        super();
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setList(List<T> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void remove(T t) {
        mList.remove(t);
        notifyDataSetChanged();
    }

    public void add(T t) {
        mList.add(t);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getItemView(position, convertView, parent);
    }

    public abstract View getItemView(int position, View convertView, ViewGroup parent);
}
