Load cursor To Array - Android android.database

Android examples for android.database:Cursor

Description

Load cursor To Array

Demo Code

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Main{

    public static <T extends Media> ArrayList<T> cursorToArray(Class<T> c,
            Cursor cursor, Context context) {
        // make sure the cursor has info
        if (cursor.getCount() == 0) {
            return null;
        }/*from w w  w . j a  v  a 2 s  . c  om*/

        cursor.moveToFirst();
        ArrayList<T> list = new ArrayList<T>();

        // loop over the cursor
        do {
            try {
                // get the constructor that takes a context
                Constructor constructor = c.getConstructor(Context.class);

                // create a new instance of the class passed
                T item = c.cast(constructor.newInstance(context));

                // get the setcursordata method from the class
                Method setCursorData = c.getMethod("setCursorData",
                        Cursor.class);
                try {
                    // invoke the method, passing the cursor
                    setCursorData.invoke(item, cursor);
                    // add the new item
                    list.add(item);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            // keep going until the end
        } while (cursor.moveToNext());
        //return the list
        return list;
    }

}

Related Tutorials