BaseAdapter适配器工厂类

写了一个BaseAdapter的公共类,以后可以直接继承,避免了重复的写不同的适配器:

public abstract class ArrayListAdapter<T> extends BaseAdapter{

    

    protected ArrayList<T> mList;

    protected Activity mContext;

    protected ListView mListView;

    

    public ArrayListAdapter(Activity context){

        this.mContext = context;

    }



    @Override

    public int getCount() {

        if(mList != null)

            return mList.size();

        else

            return 0;

    }



    @Override

    public Object getItem(int position) {

        return mList == null ? null : mList.get(position);

    }



    @Override

    public long getItemId(int position) {

        return position;

    }



    @Override

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

    

    public void setList(ArrayList<T> list){

        this.mList = list;

        notifyDataSetChanged();

    }

    

    public ArrayList<T> getList(){

        return mList;

    }

    

    public void setList(T[] list){

        ArrayList<T> arrayList = new ArrayList<T>(list.length);  

        for (T t : list) {  

            arrayList.add(t);  

        }  

        setList(arrayList);

    }

    

    public ListView getListView(){

        return mListView;

    }

    

    public void setListView(ListView listView){

        mListView = listView;

    }



}

 

 

 下面是引用上面Adapter的例子:

public class AlbumAdapter extends ArrayListAdapter<Album> {

    

    public AlbumAdapter(Activity context) {

        super(context);

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        View row=convertView;



        ViewHolder holder;



        if (row==null) {

            LayoutInflater inflater = mContext.getLayoutInflater();

            row=inflater.inflate(R.layout.album_row, null);



            holder = new ViewHolder();

            holder.image = (RemoteImageView)row.findViewById(R.id.AlbumRowImageView);

            holder.albumText = (TextView)row.findViewById(R.id.AlbumRowAlbumTextView);

            holder.artistText = (TextView)row.findViewById(R.id.AlbumRowArtistTextView);

            holder.progressBar = (ProgressBar)row.findViewById(R.id.AlbumRowRatingBar);



            row.setTag(holder);

        }

        else{

            holder = (ViewHolder) row.getTag();

        }

        

        holder.image.setDefaultImage(R.drawable.no_cd);

        holder.image.setImageUrl(mList.get(position).getImage(),position, getListView());

        holder.albumText.setText(mList.get(position).getName());

        holder.artistText.setText(mList.get(position).getArtistName());

        holder.progressBar.setMax(10);

        holder.progressBar.setProgress((int) (mList.get(position).getRating()*10));



        return row;

    }

    

    /**

     * Class implementing holder pattern,

     * performance boost

     * 

     * @author Lukasz Wisniewski

     */

    static class ViewHolder {

        RemoteImageView image;

        TextView albumText;

        TextView artistText;

        ProgressBar progressBar;

    }

}

只需重写getView()方法,设置不同的布局,和显示不同的值就可以了。

你可能感兴趣的:(BaseAdapter)