Get the values from a cursor in an array list of hash tables where the key is the name - Android Database

Android examples for Database:Cursor Get

Description

Get the values from a cursor in an array list of hash tables where the key is the name

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.HashMap;
import android.database.Cursor;

public class Main {
    /**/* w w  w.  j  a  v a2  s .c o m*/
     * Get the values from a cursor in an array list
     * of hash tables where the key is the name 
     * @param results
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getResultsTable(
            Cursor results) {

        ArrayList<HashMap<String, Object>> values = new ArrayList<HashMap<String, Object>>();

        String[] columnNames = null;

        if (results.moveToFirst()) {

            do {

                HashMap<String, Object> ret = new HashMap<String, Object>();

                if (columnNames == null) {
                    columnNames = results.getColumnNames();
                }

                for (int i = 0; i < columnNames.length; i++) {
                    ret.put(columnNames[i], results.getString(i));
                }

                values.add(ret);

            } while (results.moveToNext());

        }

        return values;

    }
}

Related Tutorials