Example usage for android.database Cursor getColumnCount

List of usage examples for android.database Cursor getColumnCount

Introduction

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

Prototype

int getColumnCount();

Source Link

Document

Return total number of columns

Usage

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);// w w w . j  a  v a  2  s  . c o  m
            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 2s.  co  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:com.taobao.weex.devtools.inspector.protocol.module.Database.java

/**
 * Flatten all columns and all rows of a cursor to a single array.  The array cannot be
 * interpreted meaningfully without the number of columns.
 *
 * @param cursor//  ww w  .j  av a  2 s  . c o m
 * @param limit Maximum number of rows to process.
 * @return List of Java primitives matching the value type of each column, converted to
 *      strings.
 */
private static ArrayList<String> flattenRows(Cursor cursor, int limit) {
    Util.throwIfNot(limit >= 0);
    ArrayList<String> flatList = new ArrayList<>();
    final int numColumns = cursor.getColumnCount();
    for (int row = 0; row < limit && cursor.moveToNext(); row++) {
        for (int column = 0; column < numColumns; column++) {
            switch (cursor.getType(column)) {
            case Cursor.FIELD_TYPE_NULL:
                flatList.add(null);
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                flatList.add(String.valueOf(cursor.getLong(column)));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                flatList.add(String.valueOf(cursor.getDouble(column)));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                flatList.add(blobToString(cursor.getBlob(column)));
                break;
            case Cursor.FIELD_TYPE_STRING:
            default:
                flatList.add(cursor.getString(column));
                break;
            }
        }
    }
    if (!cursor.isAfterLast()) {
        for (int column = 0; column < numColumns; column++) {
            flatList.add("{truncated}");
        }
    }
    return flatList;
}

From source file:at.bitfire.ical4android.AndroidTaskList.java

public static AndroidTaskList findByID(Account account, TaskProvider provider, AndroidTaskListFactory factory,
        long id) throws FileNotFoundException, CalendarStorageException {
    try {//from  w  w w . ja v  a2  s .  co  m
        @Cleanup
        Cursor cursor = provider.client.query(
                syncAdapterURI(ContentUris.withAppendedId(provider.taskListsUri(), id), account), null, null,
                null, null);
        if (cursor != null && cursor.moveToNext()) {
            AndroidTaskList taskList = factory.newInstance(account, provider, id);

            ContentValues values = new ContentValues(cursor.getColumnCount());
            DatabaseUtils.cursorRowToContentValues(cursor, values);
            taskList.populate(values);
            return taskList;
        } else
            throw new FileNotFoundException();
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query task list by ID", e);
    }
}

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

/**
 * Show all rows of a particular {@link Uri}.
 * /*from w w  w. j  a  v  a  2  s.  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:com.chess.genesis.data.GameDataDB.java

public static Bundle rowToBundle(final Cursor cursor, final int index, final boolean closeCursor) {
    final Bundle bundle = new Bundle();
    final String[] column = cursor.getColumnNames();

    cursor.moveToPosition(index);//w  w w.  jav  a2 s  . c o  m
    for (int i = 0, len = cursor.getColumnCount(); i < len; i++)
        bundle.putString(column[i], cursor.getString(i));
    if (closeCursor)
        cursor.close();
    return bundle;
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Rotate an image according to its EXIF data.
 *
 * @param context The Context/*  www .  jav a 2 s . c o  m*/
 * @param uri     The Uri to the image file
 * @param bitmap  The Bitmap to rotate
 * @return The rotated Bitmap
 */
@NonNull
private static Bitmap rotatePhoto(@NonNull Context context, @NonNull Uri uri, @NonNull Bitmap bitmap) {
    uri = getImageUri(context, uri);
    int rotation = 0;

    if ("file".equals(uri.getScheme())) {
        try {
            final ExifInterface exif = new ExifInterface(uri.getPath());
            switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotation = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotation = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotation = 270;
            }
        } catch (IOException e) {
            Log.e(TAG, "Failed to read EXIF data for " + uri.toString(), e);
        }
    } else {
        final ContentResolver cr = context.getContentResolver();
        final String[] projection = new String[] { MediaStore.Images.ImageColumns.ORIENTATION };
        final Cursor cursor = cr.query(uri, projection, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst() && cursor.getColumnCount() > 0) {
                    rotation = cursor.getInt(0);
                }
            } finally {
                cursor.close();
            }
        }
    }

    if (rotation != 0) {
        final Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    return bitmap;
}

From source file:org.linphone.compatibility.ApiFivePlus.java

public static Cursor getGeneralContactCursor(ContentResolver cr, String select, boolean shouldGroupBy) {
    String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME };
    String query;// www.  j av a2 s  .  c om

    query = Data.DISPLAY_NAME + " IS NOT NULL AND (" + select + ")";

    Cursor cursor = cr.query(Data.CONTENT_URI, projection, query, null,
            " lower(" + Data.DISPLAY_NAME + ") COLLATE UNICODE ASC");

    if (!shouldGroupBy || cursor == null) {
        return cursor;
    }

    MatrixCursor result = new MatrixCursor(cursor.getColumnNames());
    Set<String> groupBy = new HashSet<String>();
    while (cursor.moveToNext()) {
        String name = cursor.getString(getCursorDisplayNameColumnIndex(cursor));
        if (!groupBy.contains(name)) {
            groupBy.add(name);
            Object[] newRow = new Object[cursor.getColumnCount()];

            int contactID = cursor.getColumnIndex(Data.CONTACT_ID);
            int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME);

            newRow[contactID] = cursor.getString(contactID);
            newRow[displayName] = cursor.getString(displayName);

            result.addRow(newRow);
        }
    }
    cursor.close();
    return result;
}

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

public static String getRowString(Cursor c) {
    if (c == null) {
        return null;
    }/*ww w .java 2s  . co  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:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static HashSet<String> getBannedTrolls(Context context) {

    HashSet<String> bannedTrolls = null;

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    String q = "SELECT name FROM banned_users WHERE bandisabled=0";

    Cursor c = dbwrite.rawQuery(q, null);

    int count = c.getCount();
    if (count > 0) {

        bannedTrolls = new HashSet<String>(c.getColumnCount());
        c.moveToFirst();/* w  ww  .  jav a 2 s. c  om*/

        for (int i = 0; i < count; i++) {
            bannedTrolls.add(c.getString(0));
            c.moveToNext();
        }
    }

    c.close();
    dbwrite.close();
    db.close();

    if (bannedTrolls == null)
        bannedTrolls = new HashSet<String>(0);
    return bannedTrolls;
}