Android Open Source - android-tools Helper Cursor






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.comfreeze.lib.db.helper;
// w w  w  .j  a va 2 s . co  m
import net.comfreeze.lib.CFZApplication;

import android.database.Cursor;

import java.util.LinkedHashMap;

/**
 * Created by james on 8/27/13.
 */
public class HelperCursor {
    public static final String TAG = HelperCursor.class.getSimpleName();
    private LinkedHashMap<String, Integer> columns;

    public static <T extends HelperCursor> T factory(Class clazz, Cursor cursor) {
        T helper = null;
        try {
            helper = (T) clazz.newInstance();
            helper.map(cursor);
        } catch (InstantiationException e) {
            CFZApplication.LOG.e(TAG, "ROW HELPER EXCEPTION", e);
        } catch (IllegalAccessException e) {
            CFZApplication.LOG.e(TAG, "ROW HELPER EXCEPTION", e);
        }
        return helper;
    }

    public String[] getColumns() {
        return columns.keySet().toArray(new String[columns.size()]);
    }

    public Integer getInt(Cursor cursor, String name, int def) {
        if (columns.containsKey(name))
            return cursor.getInt(columns.get(name));
        return def;
    }

    public Long getLong(Cursor cursor, String name, long def) {
        if (columns.containsKey(name))
            return cursor.getLong(columns.get(name));
        return def;
    }

    public String getString(Cursor cursor, String name, String def) {
        if (columns.containsKey(name))
            return cursor.getString(columns.get(name));
        return def;
    }

    public byte[] getByte(Cursor cursor, String name, byte[] def) {
        if (columns.containsKey(name))
            return cursor.getBlob(columns.get(name));
        return def;
    }

    public Double getDouble(Cursor cursor, String name, double def) {
        if (columns.containsKey(name))
            return cursor.getDouble(columns.get(name));
        return def;
    }

    public Float getFloat(Cursor cursor, String name, float def) {
        if (columns.containsKey(name))
            return cursor.getFloat(columns.get(name));
        return def;
    }

    public Short getShort(Cursor cursor, String name, short def) {
        if (columns.containsKey(name))
            return cursor.getShort(columns.get(name));
        return def;
    }

    public HelperCursor addColumn(String name, int index) {
        if (null == columns)
            columns = new LinkedHashMap<String, Integer>();
        columns.put(name, index);
        return this;
    }

    public void map(Cursor cursor) {
        if (null != cursor) {
            String[] columnNames = cursor.getColumnNames();
            for (String name : columnNames) {
                addColumn(name, cursor.getColumnIndex(name));
            }
        }
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java