0

I have a small program that does something with images and videos and save them in /sd/myapp/imagesAndVideos

the thing is I want to make a Tab in my application that shows all the images and videos from my app folder which it has all the images and videos

I tried to find something but no hope spent like 6 hours searching... I only found couple and they all do the image gridView only and it's not loaded from the file ..

I want to make it similar to Instgram gridView when you see the pictures and videos together or the normal device Gallery ..

boudi
  • 682
  • 1
  • 8
  • 19
  • take a look into [this](http://stackoverflow.com/a/6548453/3326331) – Sagar Pilkhwal Sep 16 '14 at 12:03
  • I checked it .. but the thing is how he loaded both the images and video to his application in the same time Automatically ... I don't even understand why he used DataModel dbModel = new DataModel(this); thanks anyway .. – boudi Sep 16 '14 at 12:09
  • you can create a tab host, and use two grid view + async task and display the thumbnails, just google around for relevant code snippets and information – Sagar Pilkhwal Sep 16 '14 at 12:11
  • [Link 1](http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html) and [Link 2](http://androidexample.com/Tab_Layout_%7C_TabBar_-_Android_Example/index.php?view=article_discription&aid=103&aaid=125) for Tab Host tutorials – Sagar Pilkhwal Sep 16 '14 at 12:13

1 Answers1

1

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

  1. Create an abstract class name say DataItem.
  2. Now have some specefic implementation for this DataItem say ImageItem and VideoItem
  3. Create an list of items type DataItem
  4. Hook this list as a source to GirdView adpter.
  5. 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.

Techfist
  • 4,314
  • 6
  • 22
  • 32