BaseAdapter PlaceHolder pattern

2 votes · 1 comment

Simple adapter which implements for you HolderPattern.

raw ·
copy
· download
import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; /** * Simple base Adapter that lets you use Holder pattern * * @author Dawid Drozd * */ public abstract class BaseAdapter extends BaseAdapter { @Override public long getItemId(int position) { return position; } /** * Probably you should override this and use it to retrieve inflated views */ @Override public View getView(int position, View convertView, ViewGroup parent) { Object holder = null; if (convertView == null || convertView.getTag() == null) { convertView = getInflatedRow(position); holder = getNewHolder(); inflateHolder(holder, convertView); convertView.setTag(holder); } else holder = convertView.getTag(); initializeHolder(holder, convertView, position); return convertView; } /** * Here you must only inflate new view of a row * * @return new inflated view */ public abstract View getInflatedRow(int position); /** * Only return your new Holder that implements this protected Holder @see * {@link Holder} * * @return */ protected abstract Object getNewHolder(); /** * Initialize your fields with values for example textview.setText() and * other jobs here * * @param holder * @param view * @param position */ protected abstract void initializeHolder(Object yourHolder, View view, int position); /** * Simply initiate you fields in holder by findingViewById in view object * * @param holder * @param view */ protected abstract void inflateHolder(Object yourHolder, View view); }
Add a comment

1 Comment

Of course whe you override getView you must call super.getView() !

Reply · Feb. 3, 2013, 11:44 a.m.