Example usage for android.database Cursor getType

List of usage examples for android.database Cursor getType

Introduction

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

Prototype

int getType(int columnIndex);

Source Link

Document

Returns data type of the given column's value.

Usage

From source file:org.aquabase.DetailsFragment.java

public void setData(Cursor cursor, Vector<Pair<String, Integer>> columns) {
    mLabelIds.clear();// ww  w .  ja v a2 s. co m
    mValues.clear();

    for (Pair<String, Integer> pair : columns) {
        String columnName = pair.first;
        String value = new String();

        int columnIndex = cursor.getColumnIndexOrThrow(columnName);
        switch (cursor.getType(columnIndex)) {
        case Cursor.FIELD_TYPE_STRING:
            value = cursor.getString(columnIndex);
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            value = Double.toString(cursor.getDouble(columnIndex));
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            value = Integer.toString(cursor.getInt(columnIndex));
            break;
        default:
        }

        mLabelIds.add(pair.second);
        mValues.add(value);
    }
}

From source file:com.android.messaging.FakeContentProvider.java

@Override
public Cursor query(final Uri uri, final String[] projection, final String selection,
        final String[] selectionArgs, final String sortOrder) {
    LogUtil.w(LogUtil.BUGLE_TAG,//from www.  j a  v  a2  s . co  m
            "FakeContentProvider: query " + uri.toString() + " for "
                    + (projection == null ? null : TextUtils.join(",", projection)) + " where " + selection
                    + " with " + (selectionArgs == null ? null : TextUtils.join(";", selectionArgs)));

    for (final ContentOverride content : mOverrides) {
        if (content.match(uri.toString(), selection, selectionArgs)) {
            return new FakeCursor(projection, content.columns, content.data);
        }
    }
    if (mProvider != null) {
        try {
            LogUtil.w(LogUtil.BUGLE_TAG, "FakeContentProvider: delgating");

            final Cursor cursor = mProvider.query(uri, projection, selection, selectionArgs, sortOrder);

            LogUtil.w(LogUtil.BUGLE_TAG,
                    "FakeContentProvider: response size " + cursor.getCount() + " contains "
                            + TextUtils.join(",", cursor.getColumnNames()) + " type(0) " + cursor.getType(0));

            return cursor;
        } catch (final RemoteException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.facebook.stetho.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/*from w w  w .  j av  a2  s.  c o  m*/
 * @param limit Maximum number of rows to process.
 * @return List of Java primitives matching the value type of each column.
 */
private List<Object> flattenRows(Cursor cursor, int limit) {
    Util.throwIfNot(limit >= 0);
    List<Object> flatList = new ArrayList<Object>();
    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(cursor.getLong(column));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                flatList.add(cursor.getDouble(column));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                flatList.add(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:org.chromium.chrome.browser.util.CompatibilityFileProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor source = super.query(uri, projection, selection, selectionArgs, sortOrder);

    String[] columnNames = source.getColumnNames();
    String[] newColumnNames = columnNamesWithData(columnNames);
    if (columnNames == newColumnNames)
        return source;

    MatrixCursor cursor = new MatrixCursor(newColumnNames, source.getCount());

    source.moveToPosition(-1);//from w  ww .j a v  a 2  s . c o  m
    while (source.moveToNext()) {
        MatrixCursor.RowBuilder row = cursor.newRow();
        for (int i = 0; i < columnNames.length; i++) {
            switch (source.getType(i)) {
            case Cursor.FIELD_TYPE_INTEGER:
                row.add(source.getInt(i));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                row.add(source.getFloat(i));
                break;
            case Cursor.FIELD_TYPE_STRING:
                row.add(source.getString(i));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                row.add(source.getBlob(i));
                break;
            case Cursor.FIELD_TYPE_NULL:
            default:
                row.add(null);
                break;
            }
        }
    }

    source.close();
    return cursor;
}

From source file:com.rjfun.cordova.sms.SMSPlugin.java

private JSONObject getJsonFromCursor(Cursor cur) {
    JSONObject json = new JSONObject();

    int nCol = cur.getColumnCount();
    String keys[] = cur.getColumnNames();

    try {/* w  w w.  j  a v a2  s  .co  m*/
        for (int j = 0; j < nCol; j++) {
            switch (cur.getType(j)) {
            case Cursor.FIELD_TYPE_NULL:
                json.put(keys[j], null);
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                json.put(keys[j], cur.getLong(j));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                json.put(keys[j], cur.getFloat(j));
                break;
            case Cursor.FIELD_TYPE_STRING:
                json.put(keys[j], cur.getString(j));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                json.put(keys[j], cur.getBlob(j));
                break;
            }
        }
    } catch (Exception e) {
        return null;
    }

    return json;
}

From source file:org.chromium.chrome.browser.util.ChromeFileProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Uri fileUri = getFileUriWhenReady(uri);
    if (fileUri == null)
        return null;

    // Workaround for a bad assumption that particular MediaStore columns exist by certain third
    // party applications.
    // http://crbug.com/467423.
    Cursor source = super.query(fileUri, projection, selection, selectionArgs, sortOrder);

    String[] columnNames = source.getColumnNames();
    String[] newColumnNames = columnNamesWithData(columnNames);
    if (columnNames == newColumnNames)
        return source;

    MatrixCursor cursor = new MatrixCursor(newColumnNames, source.getCount());

    source.moveToPosition(-1);//from w  w w  . j av a2s  . co  m
    while (source.moveToNext()) {
        MatrixCursor.RowBuilder row = cursor.newRow();
        for (int i = 0; i < columnNames.length; i++) {
            switch (source.getType(i)) {
            case Cursor.FIELD_TYPE_INTEGER:
                row.add(source.getInt(i));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                row.add(source.getFloat(i));
                break;
            case Cursor.FIELD_TYPE_STRING:
                row.add(source.getString(i));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                row.add(source.getBlob(i));
                break;
            case Cursor.FIELD_TYPE_NULL:
            default:
                row.add(null);
                break;
            }
        }
    }

    source.close();
    return cursor;
}

From source file:name.zurell.kirk.apps.android.rhetolog.RhetologApplication.java

/** Data management support routines */

public Uri insertContactIntoParticipants(Uri contact, Uri session) {

    String[] contactProjection = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };

    Cursor contactQuery = getContentResolver().query(contact, contactProjection, null, null, null);

    if ((contactQuery == null) || (!contactQuery.moveToFirst())) {
        return null;
    }//from w ww  . j av  a2 s. co  m

    int nameCol = contactQuery.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    int photoCol = contactQuery.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI);

    String participantName = null;
    String participantPhoto = null;

    if (contactQuery.getType(nameCol) == Cursor.FIELD_TYPE_STRING)
        participantName = contactQuery.getString(nameCol);
    else
        participantName = getResources().getString(R.string.defaultParticipantCaption);

    if (contactQuery.getType(photoCol) == Cursor.FIELD_TYPE_STRING) {
        participantPhoto = contactQuery.getString(photoCol);
    } else {
        participantPhoto = "android.resource://" + this.getPackageName() + "/"
                + Integer.toString(R.drawable.rhetolog_participant);
    }

    contactQuery.close();

    ContentValues values = new ContentValues();
    values.put(RhetologContract.ParticipantsColumns.NAME, participantName);
    values.put(RhetologContract.ParticipantsColumns.PHOTO, participantPhoto);
    values.put(RhetologContract.ParticipantsColumns.LOOKUP, contact.toString());

    // Learn session id, use in participants/session/id
    //      String sessionId = session.getLastPathSegment();
    //      long currentSessionId;
    //      if (sessionId.contentEquals("currentsession")) {
    //         Bundle currentSessionBundle = getContentResolver().call(RhetologContract.PROVIDER_URI, "getCurrentSession", null, null);
    //         currentSessionId = currentSessionBundle.getLong(RhetologContentProvider.CURRENTSESSIONEXTRA);
    //      } else {
    //         currentSessionId = Long.valueOf(sessionId);
    //      }
    //      
    //      // Ugly, not sure how to make less so.
    //      values.put(RhetologContract.ParticipantsColumns.SESSION, currentSessionId);

    Uri newParticipant = getContentResolver().insert(session, values);

    return newParticipant;

}

From source file:com.dileepindia.cordova.sms.SMSPlugin.java

private JSONObject getJsonFromCursor(Cursor cur) {
    JSONObject json = new JSONObject();

    int nCol = cur.getColumnCount();
    String[] keys = cur.getColumnNames();
    try {//from  ww  w  .  j  a v  a2s  .c o  m
        for (int j = 0; j < nCol; j++) {
            switch (cur.getType(j)) {
            /*case 0: 
            json.put(keys[j], null);
            break;
            case 1: 
            json.put(keys[j], cur.getInt(j));
            break;
                      
            case 2: 
            json.put(keys[j], cur.getLong(j));
            break;
            case 3: 
            json.put(keys[j], cur.getFloat(j));
            break;
            case 4: 
            json.put(keys[j], cur.getString(j));
            break;
                     
            case 5: 
            json.put(keys[j], cur.getBlob(j));
              */
            case Cursor.FIELD_TYPE_BLOB:
                json.put(keys[j], cur.getBlob(j).toString());
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                json.put(keys[j], cur.getDouble(j));
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                json.put(keys[j], cur.getLong(j));
                break;
            case Cursor.FIELD_TYPE_NULL:
                json.put(keys[j], cur);
                break;
            case Cursor.FIELD_TYPE_STRING:
                json.put(keys[j], cur.getString(j));
                break;

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return json;
}

From source file:com.phonegap.plugins.sqlitePlugin.SQLitePlugin.java

/**
 * Process query results./* w  w w. jav  a  2 s . c o m*/
 *
 * @param cur
 *            Cursor into query results
 * @param tx_id
 *            Transaction id
 */
public void processResults(Cursor cur, String query_id, String tx_id) {

    String result = "[]";
    // If query result has rows

    if (cur.moveToFirst()) {
        JSONArray fullresult = new JSONArray();
        String key = "";
        int colCount = cur.getColumnCount();

        // Build up JSON result object for each row
        do {
            JSONObject row = new JSONObject();
            try {
                for (int i = 0; i < colCount; ++i) {
                    key = cur.getColumnName(i);
                    if (android.os.Build.VERSION.SDK_INT >= 11) {
                        switch (cur.getType(i)) {
                        case Cursor.FIELD_TYPE_NULL:
                            row.put(key, null);
                            break;
                        case Cursor.FIELD_TYPE_INTEGER:
                            row.put(key, cur.getInt(i));
                            break;
                        case Cursor.FIELD_TYPE_FLOAT:
                            row.put(key, cur.getFloat(i));
                            break;
                        case Cursor.FIELD_TYPE_STRING:
                            row.put(key, cur.getString(i));
                            break;
                        case Cursor.FIELD_TYPE_BLOB:
                            row.put(key, cur.getBlob(i));
                            break;
                        }
                    } else {
                        row.put(key, cur.getString(i));
                    }
                }
                fullresult.put(row);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (cur.moveToNext());

        result = fullresult.toString();
    }
    if (query_id.length() > 0)
        this.sendJavascript(" SQLitePluginTransaction.queryCompleteCallback('" + tx_id + "','" + query_id
                + "', " + result + ");");

}

From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java

private SQLitePLuginResult doSelectInBackgroundAndPossiblyThrow(String sql, String[] bindArgs,
        SQLiteDatabase db) {/*w  w  w.j  av  a2  s .  c  o m*/
    debug("\"all\" query: %s", sql);
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, bindArgs);
        int numRows = cursor.getCount();
        if (numRows == 0) {
            return EMPTY_RESULT;
        }
        int numColumns = cursor.getColumnCount();
        Object[][] rows = new Object[numRows][];
        String[] columnNames = cursor.getColumnNames();
        for (int i = 0; cursor.moveToNext(); i++) {
            Object[] row = new Object[numColumns];
            for (int j = 0; j < numColumns; j++) {
                row[j] = getValueFromCursor(cursor, j, cursor.getType(j));
            }
            rows[i] = row;
        }
        debug("returning %d rows", numRows);
        return new SQLitePLuginResult(rows, columnNames, 0, 0, null);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}