Why dont you use Inheritance to acheive the same, let me drill down an explain the same to you.
First Girdview or Listview all works on Adapter design pattern, saying which means all AdapterViews are hooked upon a source of data to which corresponding endless List are generated for display.
Now say, you want to display X types of contents in a List or Gird, for this what you can do is
- Create an abstract class name say DataItem.
- Now have some specefic implementation for this DataItem say ImageItem and VideoItem
- Create an list of items type DataItem
- Hook this list as a source to GirdView adpter.
- Now in getView all you need to perform is, to check type of DataItem it is, and return corresponding type of view to be displayed.
with above approach a type of Grid which you need can be generated, now this design can be further improved.
You can setTag the type of view it is before returning it from getView from adapter, now once you receive the convertView, just check if tag type matches the current item type, if then then easy use the same convertView else inflate a new one of corresponding type and set it as a tag and so on.
Hope this will give you an idea,
you can comment back in case you need some codes as well.
here is an dummy impementation.
// this is your main abstract class
public abstract class DataItem {
// You can define your data types here
public static final int DATA_TYPE_IMAGE = 0;
public static final int DATA_TYPE_VIDEO = 1;
// have all data item implement this
public abstract int getDataType();
public abstract String getContentUri();
public abstract View getChildView(Context context,View convertView);
}
// sample implementation for defining new image type data item
public class ImageItem extends DataItem {
public String contentUri;
public void setContenturi(String str){
this.contentUri = str;
}
@Override
public int getDataType() {
// return corresponding content type
return DataItem.DATA_TYPE_IMAGE;
}
@Override
public String getContentUri() {
// Return URI of image or video, to fetch information related to it
return this.contentUri;
}
@Override
public View getChildView(Context context,View convertView) {
// return the view you want to display for this data item in grid
// use content uri to fetch and populate Data and then generate view and return
// use convertView is not null
return null;
}
static class ImageData extends Data{
// have some variables here which will define attributes of this Data item
// in case of image
String imageName;
long takenData;
}
}
// your sample adapter
public class GridAdapter extends BaseAdapter {
ArrayList<DataItem> mList;
Context context;
public GridAdapter(ArrayList<DataItem> items, Context context){
this.mList = items;
this.context = context;
}
public void setList(ArrayList<DataItem> items){
this.mList = items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DataItem item = mList.get(position);
if(convertView != null){
// first check if existing view can be used for diaply or nor
int tag = (Integer)convertView.getTag();
if(tag == item.getDataType()){
// alright both are of same type
return item.getChildView(context,convertView);
}else{
// get the new view for corresponding data, set it as tag and return
View view = item.getChildView(context,null);
view.setTag(new Integer(item.getDataType()));
return view;
}
}else{
View view = item.getChildView(context,null);
view.setTag(new Integer(item.getDataType()));
return view;
}
}
}
this would be enough i guess to throw a pointer.