在项目的代码中有一个包com.teleca.jamendo.adapter专门存放使用到的所有Adapter,这样确实方便管理,当然代码里面最好的还是这些Adapter的集成关系,是代码清晰而且容易复用。所有的Adapter都是集成自这个抽象的自定义的ArrayListAdapter
/**
* Nice wrapper-abstraction around ArrayList
*
* @author Lukasz Wisniewski
*
* @param <T>
*/
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;
}
}
正如作者所说Nice wrapper-abstraction around ArrayList很好的封装了ArrayList的抽象类
/**
* Adapter representing albums
*
* @author Lukasz Wisniewski
*/
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;
}
}
当使用时:
AlbumAdapter albumAdapter = new AlbumAdapter(SearchActivity.this);
albumAdapter.setList(albums);
albumAdapter.setListView(mSearchListView);
也就是设置父类里面的三个属性的值
protected ArrayList<T> mList;
protected Activity mContext;
protected ListView mListView;
备注:ProgressBar的一点新的发现
holder.progressBar.setMax(10);
holder.progressBar.setProgress((int) (mList.get(position).getRating()*10));
原来ProgressBar可以当rating来使用