ListView或GrideView的Adapter父类

public abstract class AdapterBase<T> extends android.widget.BaseAdapter {
    protected List<T> list;
    protected Context context;
    protected LayoutInflater inflater;

    public AdapterBase(Context context, List<T> list) {
        this.list = list;
        this.context = context;
        this.inflater = inflater.from(context);
    }

    protected <V> V findView(Activity paramActivity, int paramInt) {
        return (V) paramActivity.findViewById(paramInt);
    }

    protected <V> V findView(View paramView, int paramInt) {
        return (V) paramView.findViewById(paramInt);
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public T getItem(int i) {
        if (getCount() != 0) {
            return list.get(i);
        }
        return null;
    }

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


    @Override
    public abstract View getView(int position, View convertView, ViewGroup viewGroup);

    public void refresh(List<T> data) {
        if (data != null) {
            if (data.size() > 0) {
                if (list==data) { // 两个list 是同一个
                    notifyDataSetChanged();
                    return;
                } else {
                    list.clear();
                    list.addAll(data);
                }
            } else {
                list.clear();
            }
        } else {
            list.clear();
        }
        notifyDataSetChanged();
    }


    /**
     * 清理List集合
     */
    public void clean() {
        list.clear();
        notifyDataSetChanged();
    }

    /**
     * 添加集合
     *
     * @param list
     */
    public void addAll(List<T> list) {
        if (list != null) {
            if (list.size() > 0) {
                if (this.list.equals(list)) { // 两个list 是同一个
                    notifyDataSetChanged();
                    return;
                } else {
                    this.list.addAll(list);
                }
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 添加集合到指定位置
     *
     * @param list
     * @param position
     */
    public void addAll(List<T> list, int position) {
        this.list.addAll(position, list);
        notifyDataSetChanged();
    }

    /**
     * 返回List集合
     *
     * @return
     */
    public List<T> getList() {
        return list;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值