UPDATED: No more bugs! Comment on the sample itself. Advantages and disadvantages. What do you like, what you do not do. What can be fixed. Still don't understand why I did this ... let me know (but read my post below)
I am working on creating a new design pattern for BaseAdapters on Android, and so far I really like how it works!
I have a data structure that can contain all the necessary data for each object in a particular type of collection. I am creating specific implementations of the BaseAdapter with my own user interface layout that will be displayed to the user on the screen. Pretty simple and great idea ... nothing new.
OK, so WHY do I want this?
The general idea is: 1) to ignore the implementation of the getView method and allow more amateur developers to easily create their own custom adapters and 2) to simplify and abstract as much as possible.
NOTE: in the last two parts of the code, I have only 3 things to implement (this makes sense). The inner class that is present in the current Holder template is the SetLayoutResource (...) method and the ExtractLayoutResources (...) method.
3 main reasons why I want this and think itβs good ... abstraction, abstraction, abstraction!
ConcreteCustomAdapter.java
(A specific code example ... this is basically how everything will look !!! This is basically all you need to enter to create a new custom base adapter !!!)
public class ConcreteCustomAdapter extends BaseDataAdapter<Song, SongHolder> { public ConcreteCustomAdapter(Context context, int resource, Song[] data) { super(context, resource, data);
Song.java
(User created!)
// Entity that holds ALL the data public class Song implements IData { public int thumbnail; public String Name; ... // Constructors, Getters, Setters ... }
LEISURE CODES TO SUPPORT GOALS
IHolder.java
// Current Adapter Pattern uses Holder Objects, this represents that and the data via interface public interface IHolder { interface IData { } }
BaseDataAdapter.java
(User should not touch this)
// D for Data....H for Holder (sorry not convention) public abstract class BaseDataAdapter<D extends IData, H extends IHolder> extends BaseAdapter { private Context context; private int layoutResourceID; private D data[] = null; private H holder = null; public BaseDataAdapter(Context context, int resource, D[] data) { //super(context, resource, data); this.layoutResourceID = resource; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { //super.getView(position, convertView, parent); View row = convertView; if (row == null) { LayoutInflater newView = ((Activity) context).getLayoutInflater(); row = newView.inflate(this.layoutResourceID, parent, false); extractLayoutResources(row, holder); row.setTag(holder); } else { holder = (H) row.getTag(); } setLayoutResources(holder, data[position]); return row; } public void setViewHolder(H holder) { this.holder = holder; } abstract protected void setLayoutResources(H holder, D data); abstract protected void extractLayoutResources(View row, H holder); @Override public int getCount() { return this.data.length; } @Override public Object getItem(int position) { return this.data[position]; } @Override public long getItemId(int position) { return 0; } }