Database cursor To Hash Map - Android Database

Android examples for Database:Cursor Convert

Description

Database cursor To Hash Map

Demo Code


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

public class Main {
    public static ArrayList<HashMap<String, String>> cursorToHashMap(
            Cursor cursor) {// w w  w  . j av  a2  s.  c  o  m

        if (cursor != null) {
            int cursorCount = cursor.getCount();
            int columnCount;
            ArrayList<HashMap<String, String>> cursorData = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> rowHashMap;
            for (int i = 0; i < cursorCount; i++) {
                cursor.moveToPosition(i);
                rowHashMap = new HashMap<String, String>();
                columnCount = cursor.getColumnCount();
                for (int j = 0; j < columnCount; j++) {
                    rowHashMap.put(cursor.getColumnName(j),
                            cursor.getString(j));
                }
                cursorData.add(rowHashMap);
            }
            cursor.close();

            return cursorData;
        } else {
            return null;
        }
    }
}

Related Tutorials