Returns an indexed map of objects of the specified class via Reflection. - Android java.lang.reflect

Android examples for java.lang.reflect:Object Type

Description

Returns an indexed map of objects of the specified class via Reflection.

Demo Code


//package com.book2s;
import java.lang.reflect.Field;

import java.util.LinkedHashMap;

import java.util.Map;

import android.database.Cursor;
import android.util.Log;

public class Main {
    /**/* w  ww . j a v  a  2  s . co m*/
     * Returns an indexed map of objects of the specified class. The database cursor is used for retrieving the objects.
     * The default constructor is needed for instantiation. The fields will be filled with the help of reflection.
     * 
     * @param clazz
     *            class of the result objects
     * @param indexColumn
     *            column name for map key
     * @param cursor
     *            cursor for the result set
     * @return indexed map of objects
     */
    @SuppressWarnings("unchecked")
    public static <T, U> Map<T, U> createIndexedMap(Class<U> clazz,
            String indexColumn, Cursor cursor) {
        Map<T, U> indexedMap = new LinkedHashMap<T, U>();

        try {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                U instance = clazz.newInstance();
                T key = null;

                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    if (!cursor.isNull(i)) {
                        String columnName = cursor.getColumnName(i);
                        Field field = clazz.getDeclaredField(columnName);
                        field.setAccessible(true);

                        Object attribute = castType(field.getType(),
                                cursor, i);
                        field.set(instance, attribute);

                        if (columnName.equals(indexColumn)) {
                            key = (T) attribute;
                        }
                    }
                }
                indexedMap.put(key, instance);
                cursor.moveToNext();
            }
            cursor.close();
        } catch (Exception e) {
            Log.e(e.getClass().getSimpleName(), e.getLocalizedMessage());
        }

        return indexedMap;
    }

    private static Object castType(Class<?> paramClazz, Cursor cursor,
            int colNumber) {
        Object obj = null;
        if (paramClazz == String.class) {
            obj = cursor.getString(colNumber);
        } else if (paramClazz == Integer.class || paramClazz == int.class) {
            obj = cursor.getInt(colNumber);
        } else if (paramClazz == Long.class || paramClazz == long.class) {
            obj = cursor.getLong(colNumber);
        } else if (paramClazz == Float.class || paramClazz == float.class) {
            obj = cursor.getFloat(colNumber);
        } else if (paramClazz == Double.class || paramClazz == double.class) {
            obj = cursor.getDouble(colNumber);
        }
        return obj;
    }
}

Related Tutorials