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 extends android.widget.BaseAdapter {  
    private List list;  
  
    private LayoutInflater inflater;
  
    public BAdapter(Context context) {  
        init(context, new ArrayList());  
    }  
  
    public BAdapter(Context context, List list) {  
        init(context, list);  
    }  
  
    private void init(Context context, List list) {  
        this.list = list;  
        this.inflater = LayoutInflater.from(context);
    }  
  
    public List getList() {  
        return list;  
    }  
  
    public void setList(List list) {  
        this.list = list;  
    }  
  
    public void clear() {  
        this.list.clear();  
        notifyDataSetChanged();  
    }  
  
    public void addAll(List 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 返回 
     */  
    @SuppressWarnings("unchecked")  
    protected  E get(View view, int id) {  
        SparseArray viewHolder = (SparseArray) view.getTag();  
        if (null == viewHolder) {  
            viewHolder = new SparseArray();  
            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是你的实体类

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
{ public AddressAdapter(Context context, List
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,Base,基类)