android BaseAdapter 自定义适配器 BaseAdapter基类抽取 二

android BaseAdapter 自定义适配器 BaseAdapter基类抽取 二

基类继承BaseAdapter

package com.zcycjy.mobile.adapter;

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

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
  
public abstract class BAdapter<T> extends android.widget.BaseAdapter {  
    private List<T> list;  
  
    private LayoutInflater inflater;
  
    public BAdapter(Context context) {  
        init(context, new ArrayList<T>());  
    }  
  
    public BAdapter(Context context, List<T> list) {  
        init(context, list);  
    }  
  
    private void init(Context context, List<T> list) {  
        this.list = list;  
        this.inflater = LayoutInflater.from(context);
    }  
  
    public List<T> getList() {  
        return list;  
    }  
  
    public void setList(List<T> list) {  
        this.list = list;  
    }  
  
    public void clear() {  
        this.list.clear();  
        notifyDataSetChanged();  
    }  
  
    public void addAll(List<T> list) {  
        if (null != list) {  
            this.list.addAll(list);  
            notifyDataSetChanged();  
        }  
    }  
    
    public void remove(int position){
    	if (null != list) {  
    		this.list.remove(position);
    		notifyDataSetChanged();
    	}
    }
  
    @Override  
    public int getCount() {  
        return list == null ? 0 : list.size();  
    }  
  
    @Override  
    public T getItem(int position) {  
        return list.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        if (null == convertView) {  
            convertView = inflate(getContentView());  
        }  
        onInitView(convertView, position);  
        return convertView;  
    }  
  
    /** 加载布局 */  
    private View inflate(int layoutResID) {  
        View view = inflater.inflate(layoutResID, null);  
        return view;  
    }  
  
    public abstract int getContentView();  
  
    public abstract void onInitView(View view, int position);  
  
    /** 
     *  
     * @param view 
     *            converView 
     * @param id 
     *            控件的id 
     * @return 返回<T extends View> 
     */  
    @SuppressWarnings("unchecked")  
    protected <E extends View> E get(View view, int id) {  
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();  
        if (null == viewHolder) {  
            viewHolder = new SparseArray<View>();  
            view.setTag(viewHolder);  
        }  
        View childView = viewHolder.get(id);  
        if (null == childView) {  
            childView = view.findViewById(id);  
            viewHolder.put(id, childView);  
  
        }  
        return (E) childView;  
    }  

}
继承基类extends BAdapter<Address>  Address是你的实体类

package com.zcycjy.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.zcycjy.mobile.R;
import com.zcycjy.mobile.entity.Address;

public class AddressAdapter extends BAdapter<Address> {

	public AddressAdapter(Context context, List<Address> list) {
		super(context, list);
	}

	@Override
	public int getContentView() {
		return R.layout.item_address;
	}

	@Override
	public void onInitView(View view, int position) {
		// TODO Auto-generated method stub
		TextView tv = get(view, R.id.item_tv_name);
		Address ad = (Address) getItem(position);
		tv.setText(ad.getId());
	}

}


你可能感兴趣的:(android,BaseAdapter,Android开发,自定义适配器,BaseAdapter基类)