Returns an java.util.LinkedList of the android.database.Cursor and closes it. - Android Database

Android examples for Database:Cursor

Description

Returns an java.util.LinkedList of the android.database.Cursor and closes it.

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;

public class Main{
    /**/*from w  w  w.  j  a  v  a  2s  . com*/
     * Returns an {@link java.util.LinkedList} of the {@link android.database.Cursor} and closes
     * it.
     */
    public static <T> LinkedList<T> consumeToLinkedList(
            IterableCursor<T> cursor) {
        return consumeToCollection(cursor, new LinkedList<T>());
    }
    /**
     * Add each item of this {@link android.database.Cursor} to the {@code collection} parameter.
     * Closes the cursor once completed.
     *
     * @return the same collection as the parameter.
     * @see #consumeToArrayList(com.venmo.cursor.IterableCursor)
     * @see #consumeToLinkedList(com.venmo.cursor.IterableCursor)
     * @see #consumeToLinkedHashSet(com.venmo.cursor.IterableCursor)
     */
    public static <T, C extends Collection<T>> C consumeToCollection(
            IterableCursor<T> cursor, C collection) {
        try {
            for (T t : cursor) {
                collection.add(t);
            }
        } finally {
            cursor.close();
        }
        return collection;
    }
}

Related Tutorials