Question

I'm currently trying to get the hang of android developemnt and so far its been going good(Well until now). I am currently playing around with Custom Cursor adapters for my grid view. I read using a cursor adapter takes the load of the main UI thread which sounds efficient, so i tried to implement my own cursor adapter as follows

public class VideoListAdapter extends SimpleCursorAdapter{

private Context ctx;
private int parent_layout_id;

ArrayList<VideoModel> videoList = new ArrayList<VideoModel>();

public VideoListAdapter(
        Context ctx,
        int parent_layout_id,
        Cursor c,
        String[] fromColumns,
        int[] toView)
{
    super(ctx,parent_layout_id,c,fromColumns,toView);
    this.ctx = ctx;
    this.parent_layout_id = parent_layout_id;
}

@Override
public void bindView(View v, Context context, Cursor c) {

    //gets title column
    int titleColumn = c.getColumnIndex(MediaStore.Video.Media.TITLE);
    String title = c.getString(titleColumn);
    TextView titleView =(TextView) v.findViewById(R.id.video_title);
    titleView.setText(title);

    //gets duration
    int durationColumn = c.getColumnIndex(MediaStore.Video.Media.DURATION);
    String duration = c.getString(durationColumn);
    TextView durationView = (TextView) v.findViewById(R.id.video_date);
    durationView.setText(duration);

    //gets Thumb
    int videoDataColumn = c.getColumnIndex(MediaStore.Video.Media.DATA);
    String videodataUrl = c.getString(videoDataColumn);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(videodataUrl);
    Bitmap bm = mmr.getFrameAtTime(1000);
    ImageView imgv = (ImageView) v.findViewById(R.id.video_thumb);
    imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imgv.setImageBitmap(bm);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Cursor c = getCursor();

    //get layout inflater
    final LayoutInflater li=LayoutInflater.from(context);
    View v = li.inflate(parent_layout_id,parent,false);

    //gets title column
    int titleColumn = c.getColumnIndex(MediaStore.Video.Media.TITLE);
    String title = c.getString(titleColumn);
    TextView titleView =(TextView) v.findViewById(R.id.video_title);
    titleView.setText(title);

    //gets duration
    int durationColumn = c.getColumnIndex(MediaStore.Video.Media.DURATION);
    String duration = c.getString(durationColumn);
    TextView durationView = (TextView) v.findViewById(R.id.video_date);
    durationView.setText(duration);

    //gets Thumb
    int videoDataColumn = c.getColumnIndex(MediaStore.Video.Media.DATA);
    String videodataUrl = c.getString(videoDataColumn);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(videodataUrl);
    Bitmap bm = mmr.getFrameAtTime(1000);
    ImageView imgv = (ImageView) v.findViewById(R.id.video_thumb);
    imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imgv.setImageBitmap(bm);
    return v;
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    return super.runQueryOnBackgroundThread(constraint);
}

}

What i'm trying to achieve is to fetch list of video files from the external media storage and display them in a gridview. the standard thumbnails are abit too small for the style of layout i have in mind so i used the MediaMetadataRetriever to fetch frames from each of the video files and used that as thumbnails. Like soGrid View

When i ran the application the scroll performance of my gridView worsened. What am I missing. i know the SimpleCursorAdapter class has an overridable method runQueryOnBackgroundThread but i dont even know what to put in there or how to use it. Any pointers will be very much appreciated and forgive my smudge face ;)

Was it helpful?

Solution

First of all bind your Gridview using ViewHolder and second one is save your thumb(bitmap) in existing arrayList so you can not do have to same process while scrolling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top