Example usage for android.database Cursor getColumnName

List of usage examples for android.database Cursor getColumnName

Introduction

In this page you can find the example usage for android.database Cursor getColumnName.

Prototype

String getColumnName(int columnIndex);

Source Link

Document

Returns the column name at the given zero-based column index.

Usage

From source file:Main.java

public static String logCursor(String prefix, Cursor cr) {
    StringBuilder sb = new StringBuilder().append(prefix + ": ");
    for (int i = 0; i < cr.getColumnCount(); i++) {
        sb.append(cr.getColumnName(i)).append("=");
        switch (cr.getType(i)) {
        case Cursor.FIELD_TYPE_NULL:
            sb.append("NULL");
            break;
        case Cursor.FIELD_TYPE_STRING:
            sb.append("\"").append(cr.getString(i)).append("\"");
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            sb.append(cr.getInt(i));// ww w.  j  a  v a 2 s  .  c  o m
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            sb.append(cr.getFloat(i));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            sb.append("BIN(").append(cr.getBlob(i).length).append("b)");
            break;
        }
        sb.append(" ");
    }
    return sb.toString();
}

From source file:Main.java

public static Map<String, Integer> getColumnMap(Cursor cursor) {
    Map<String, Integer> retMap = new HashMap<String, Integer>();
    int nCols = cursor.getColumnCount();
    for (int i = 0; i < nCols; i++) {
        retMap.put(cursor.getColumnName(i), i);
    }/* www  .  j  a  v a 2 s .co m*/
    return retMap;
}

From source file:Main.java

static public void dumpCursor(Cursor cursor) {
    final String LOG_TAG = "dumpCursor";
    if (cursor != null) {
        Log.d(LOG_TAG, "cursor " + cursor.getCount());
        for (int i = 0; i < cursor.getColumnCount(); i++)
            Log.d(LOG_TAG, "col " + i + " " + cursor.getColumnName(i));

        cursor.moveToFirst();//from  w w w.  j a  va  2  s .c  om
        String[] a = new String[cursor.getColumnCount()];
        while (!cursor.isAfterLast()) {
            for (int i = 0; i < a.length; i++)
                a[i] = cursor.getString(i);
            Log.d(LOG_TAG, Arrays.toString(a));
            cursor.moveToNext();
        }

        cursor.moveToFirst();
    }
}

From source file:Main.java

/**
 * Print the content of the cursor//from w  w w  .  j  a  va 2  s.  c  om
 *
 * @param cursor, The cursor, which content needs to be printed
 * @param logTag, The log tag
 */
public static void printCursorContent(Cursor cursor, String logTag) {
    if (cursor == null) {
        Log.d(logTag, "Cursor is NULL!");
        return;
    }
    final int columnSpace = 2;
    ArrayList<Integer> columnWidth = new ArrayList<Integer>();
    for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
        String value = cursor.getColumnName(columnIndex);
        int maxWidth = value.length();
        if (cursor.moveToFirst()) {
            do {
                try {
                    value = cursor.getString(columnIndex);
                } catch (Exception e) {
                    value = "BLOB";
                    Log.d(logTag, "Get value from " + cursor.getColumnName(columnIndex) + " failed. Caused by "
                            + e.getLocalizedMessage());
                }
                if (!TextUtils.isEmpty(value) && value.length() > maxWidth) {
                    maxWidth = value.length();
                }
            } while (cursor.moveToNext());
        }
        columnWidth.add(maxWidth + columnSpace);
    }
    ArrayList<ArrayList<String>> tableContent = new ArrayList<ArrayList<String>>();
    for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
        ArrayList<String> columnContent = new ArrayList<String>();
        String value = cursor.getColumnName(columnIndex);
        columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex)));
        if (cursor.moveToFirst()) {
            do {
                try {
                    value = cursor.getString(columnIndex);
                } catch (Exception e) {
                    value = "BLOB";
                }
                columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex)));
            } while (cursor.moveToNext());
        }
        tableContent.add(columnContent);
    }
    // Including the header
    int maxRowIndex = cursor.getCount() + 1;
    for (int rowIndex = 0; rowIndex < maxRowIndex; rowIndex++) {
        StringBuilder rowValues = new StringBuilder();
        for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
            ArrayList<String> columnValues = tableContent.get(columnIndex);
            rowValues.append(columnValues.get(rowIndex));
        }
        Log.d(logTag, rowValues.toString());
    }
    // set the cursor back the first item
    cursor.moveToFirst();
}

From source file:Main.java

public static ArrayList<HashMap<String, String>> cursorToHashMap(Cursor cursor) {

    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);/*from  w  w  w  . ja va 2  s .  c om*/
            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;
    }
}

From source file:li.barter.utils.Utils.java

/**
 * Converts a cursor to a bundle. Field datatypes will be maintained. Floats will be stored in
 * the Bundle as Doubles, and Integers will be stored as Longs due to Cursor limitationcs
 *
 * @param cursor The cursor to convert to a Bundle. This must be positioned to the row to be
 *               read//from  w w w  .j av a 2 s .  c o  m
 * @return The converted bundle
 */
public static Bundle cursorToBundle(Cursor cursor) {

    final int columnCount = cursor.getColumnCount();
    final Bundle bundle = new Bundle(columnCount);

    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {

        final String columnName = cursor.getColumnName(columnIndex);
        switch (cursor.getType(columnIndex)) {

        case Cursor.FIELD_TYPE_STRING: {
            bundle.putString(columnName, cursor.getString(columnIndex));
            break;
        }

        case Cursor.FIELD_TYPE_BLOB: {
            bundle.putByteArray(columnName, cursor.getBlob(columnIndex));
            break;
        }

        case Cursor.FIELD_TYPE_FLOAT: {
            bundle.putDouble(columnName, cursor.getDouble(columnIndex));
            break;
        }

        case Cursor.FIELD_TYPE_INTEGER: {
            bundle.putLong(columnName, cursor.getLong(columnIndex));
            break;
        }
        }
    }

    return bundle;
}

From source file:de.ub0r.android.smsdroid.ConversationListActivity.java

/**
 * Show all rows of a particular {@link Uri}.
 * //www .j  a  v a2s .c o  m
 * @param context
 *            {@link Context}
 * @param u
 *            {@link Uri}
 */
static void showRows(final Context context, final Uri u) {
    Log.d(TAG, "-----GET HEADERS-----");
    Log.d(TAG, "-- " + u.toString() + " --");
    Cursor c = context.getContentResolver().query(u, null, null, null, null);
    if (c != null) {
        int l = c.getColumnCount();
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < l; i++) {
            buf.append(i + ":");
            buf.append(c.getColumnName(i));
            buf.append(" | ");
        }
        Log.d(TAG, buf.toString());
    }

}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

public static String getRowString(Cursor c) {
    if (c == null) {
        return null;
    }/*from  w  w w  .  j  a  va 2  s  .c  o m*/

    String row = "";
    for (int i = 0; i < c.getColumnCount(); i++) {
        try {
            row = row + c.getColumnName(i) + "=" + c.getString(i) + ";";
        } catch (Exception e) {
            row = row + "@" + Integer.toString(i) + "=?;";
        }
    }
    return row;
}

From source file:eu.geopaparazzi.library.database.DbCursorAdapter.java

public void bindView(View view, Context context, Cursor cursor) {
    StringBuilder sb = new StringBuilder();
    TextView textView = (TextView) view;
    for (int i = 0; i < cursor.getColumnCount(); i++) {
        String field = cursor.getColumnName(i);
        if (field.equals("_id")) {
            continue;
        }//from   www  . j a va 2s.  co m
        String value = cursor.getString(i);
        sb.append("\n").append(field).append(" = ").append(value);
    }
    if (sb.length() > 1) {
        String text = sb.substring(1);
        textView.setText(text);
    } else {
        textView.setText(" - nv - ");
    }
    textView.setTextColor(context.getResources().getColor(R.color.main_text_color));
    textView.setPadding(5, 5, 5, 5);
}

From source file:piuk.blockchain.android.ui.SendingAddressesFragment.java

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    activity = getActivity();/*  w  w w.j ava2s  .co  m*/

    setEmptyText(getString(R.string.address_book_empty_text));

    adapter = new SimpleCursorAdapter(activity, R.layout.address_book_row, null,
            new String[] { AddressBookProvider.KEY_LABEL, AddressBookProvider.KEY_ADDRESS },
            new int[] { R.id.address_book_row_label, R.id.address_book_row_address }, 0);
    adapter.setViewBinder(new ViewBinder() {
        public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
            if (!AddressBookProvider.KEY_ADDRESS.equals(cursor.getColumnName(columnIndex)))
                return false;

            ((TextView) view).setText(cursor.getString(columnIndex));

            return true;
        }
    });
    setListAdapter(adapter);

    getLoaderManager().initLoader(0, null, this);
}