Java Code Examples for android.database.Cursor#getType()
The following examples show how to use
android.database.Cursor#getType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermissionTester.java From PermissionAgent with Apache License 2.0 | 6 votes |
public static void read(Cursor cursor) { int count = cursor.getCount(); if (count > 0) { cursor.moveToFirst(); int type = cursor.getType(0); switch (type) { case Cursor.FIELD_TYPE_BLOB: case Cursor.FIELD_TYPE_NULL: { break; } case Cursor.FIELD_TYPE_INTEGER: case Cursor.FIELD_TYPE_FLOAT: case Cursor.FIELD_TYPE_STRING: default: { cursor.getString(0); break; } } } }
Example 2
Source File: DBProxy.java From android-orm with Apache License 2.0 | 6 votes |
/** * 封装内容到Map对象 * * @param cursor * @param columnName * @param map */ private void putMapKeyValue(Cursor cursor, String columnName, Map<String, Object> map) { int columnIndex = cursor.getColumnIndex(columnName); int type = cursor.getType(columnIndex); switch (type) { case Cursor.FIELD_TYPE_INTEGER: map.put(columnName, cursor.getLong(columnIndex)); break; case Cursor.FIELD_TYPE_STRING: map.put(columnName, cursor.getString(columnIndex)); break; case Cursor.FIELD_TYPE_FLOAT: map.put(columnName, cursor.getFloat(columnIndex)); break; case Cursor.FIELD_TYPE_BLOB: map.put(columnName, cursor.getBlob(columnIndex)); break; case Cursor.FIELD_TYPE_NULL: map.put(columnName, cursor.getString(columnIndex)); break; default: break; } }
Example 3
Source File: OCursorListAdapter.java From hr with GNU Affero General Public License v3.0 | 6 votes |
private Object getValue(Cursor c, String column) { Object value = false; int index = c.getColumnIndex(column); switch (c.getType(index)) { case Cursor.FIELD_TYPE_NULL: value = false; break; case Cursor.FIELD_TYPE_BLOB: case Cursor.FIELD_TYPE_STRING: value = c.getString(index); break; case Cursor.FIELD_TYPE_FLOAT: value = c.getFloat(index); break; case Cursor.FIELD_TYPE_INTEGER: value = c.getInt(index); break; } return value; }
Example 4
Source File: CalculatorProviderTest.java From SuntimesWidget with GNU General Public License v3.0 | 6 votes |
private boolean columnIsDouble(Cursor cursor, String column) { if (cursor != null) { try { int index = cursor.getColumnIndex(column); if (cursor.getType(index) == Cursor.FIELD_TYPE_FLOAT) { double value = cursor.getDouble(index); return true; } } catch (NumberFormatException e) { return false; } } return false; }
Example 5
Source File: SQLiteAndroidDatabase.java From AvI with MIT License | 6 votes |
@SuppressLint("NewApi") private void bindPostHoneycomb(JSONObject row, String key, Cursor cur, int i) throws JSONException { int curType = cur.getType(i); switch (curType) { case Cursor.FIELD_TYPE_NULL: row.put(key, JSONObject.NULL); break; case Cursor.FIELD_TYPE_INTEGER: row.put(key, cur.getLong(i)); break; case Cursor.FIELD_TYPE_FLOAT: row.put(key, cur.getDouble(i)); break; /* ** Read BLOB as Base-64 DISABLED in this branch: case Cursor.FIELD_TYPE_BLOB: row.put(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT))); break; // ** Read BLOB as Base-64 DISABLED to HERE. */ case Cursor.FIELD_TYPE_STRING: default: /* (not expected) */ row.put(key, cur.getString(i)); break; } }
Example 6
Source File: SuntimesThemeProviderTest.java From SuntimesWidget with GNU General Public License v3.0 | 6 votes |
private boolean columnIsInt(Cursor cursor, String column) { if (cursor != null) { try { int index = cursor.getColumnIndex(column); if (cursor.getType(index) == Cursor.FIELD_TYPE_INTEGER); { int value = cursor.getInt(index); return true; } } catch (NumberFormatException e) { return false; } } return false; }
Example 7
Source File: ForeignKeyCleanerImpl.java From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License | 6 votes |
private String getColumnValueAsString(Cursor cursor, String columnName) { int fromColumnIndex = cursor.getColumnIndex(columnName); int fromColumnType = cursor.getType(fromColumnIndex); String columnValue; switch (fromColumnType) { case 1: columnValue = String.valueOf(cursor.getInt(fromColumnIndex)); break; case 2: columnValue = String.valueOf(cursor.getFloat(fromColumnIndex)); break; case 3: columnValue = cursor.getString(fromColumnIndex); break; default: columnValue = null; break; } return columnValue; }
Example 8
Source File: XContentResolver.java From XPrivacy with GNU General Public License v3.0 | 6 votes |
private void _dumpColumns(Cursor cursor, String msg) { String[] columns = new String[cursor.getColumnCount()]; for (int i = 0; i < cursor.getColumnCount(); i++) switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_NULL: columns[i] = null; break; case Cursor.FIELD_TYPE_INTEGER: columns[i] = Integer.toString(cursor.getInt(i)); break; case Cursor.FIELD_TYPE_FLOAT: columns[i] = Float.toString(cursor.getFloat(i)); break; case Cursor.FIELD_TYPE_STRING: columns[i] = cursor.getString(i); break; case Cursor.FIELD_TYPE_BLOB: columns[i] = "[blob]"; break; default: Util.log(this, Log.WARN, "Unknown cursor data type=" + cursor.getType(i)); } Util.log(this, Log.WARN, TextUtils.join(", ", columns) + " " + msg); }
Example 9
Source File: DbSqlite.java From Collection-Android with MIT License | 5 votes |
/** * set data in cursor to ResultSet List * @param cursor * @param resultList the data will set in it */ private void parseCursorToResult(Cursor cursor, List<ResultSet> resultList){ int columnCount; int columnType; Object columnVal = null; while (cursor.moveToNext()) { columnCount = cursor.getColumnCount(); ResultSet result = new ResultSet(); for (int index = 0; index < columnCount; ++index) { columnType = cursor.getType(index); switch (columnType) { case Cursor.FIELD_TYPE_BLOB: columnVal = cursor.getBlob(index); break; case Cursor.FIELD_TYPE_FLOAT: columnVal = cursor.getDouble(index); break; case Cursor.FIELD_TYPE_INTEGER: columnVal = cursor.getLong(index); break; case Cursor.FIELD_TYPE_NULL: columnVal = null; break; default: columnVal = cursor.getString(index); break; } result.setValue(cursor.getColumnName(index), columnVal); } resultList.add(result); } }
Example 10
Source File: SMSReceive.java From cordova-plugin-sms-receive with MIT License | 5 votes |
private JSONObject getJsonFromCursor(Cursor cur) { JSONObject json = new JSONObject(); int nCol = cur.getColumnCount(); String keys[] = cur.getColumnNames(); try { for (int j=0; j<nCol; j++) { switch(cur.getType(j)) { case Cursor.FIELD_TYPE_NULL: json.put(keys[j], JSONObject.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; }
Example 11
Source File: DBType.java From BobEngine with GNU Lesser General Public License v2.1 | 5 votes |
public static int getType(Cursor cursor, int i) { SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor; CursorWindow cursorWindow = sqLiteCursor.getWindow(); int pos = cursor.getPosition(); int type = -1; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Honeycomb or later. type = cursor.getType(i); if (type == Cursor.FIELD_TYPE_BLOB) { type = FIELD_TYPE_BLOB; } else if (type == Cursor.FIELD_TYPE_FLOAT) { type = FIELD_TYPE_FLOAT; } else if (type == Cursor.FIELD_TYPE_INTEGER) { type = FIELD_TYPE_INTEGER; } else if (type == Cursor.FIELD_TYPE_NULL) { type = FIELD_TYPE_NULL; } else if (type == Cursor.FIELD_TYPE_STRING) { type = FIELD_TYPE_STRING; } } else { // Before Honeycomb if (cursorWindow.isNull(pos, i)) { type = FIELD_TYPE_NULL; } else if (cursorWindow.isLong(pos, i)) { type = FIELD_TYPE_INTEGER; } else if (cursorWindow.isFloat(pos, i)) { type = FIELD_TYPE_FLOAT; } else if (cursorWindow.isString(pos, i)) { type = FIELD_TYPE_STRING; } else if (cursorWindow.isBlob(pos, i)) { type = FIELD_TYPE_BLOB; } } return type; }
Example 12
Source File: SmsModule.java From react-native-get-sms-android with MIT License | 5 votes |
private JSONObject getJsonFromCursor(Cursor cur) { JSONObject json = new JSONObject(); int nCol = cur.getColumnCount(); String[] keys = cur.getColumnNames(); try { 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)); } } catch (Exception e) { return null; } return json; }
Example 13
Source File: XContentResolver.java From XPrivacy with GNU General Public License v3.0 | 5 votes |
private void copyColumns(Cursor cursor, MatrixCursor result, int count) { try { Object[] columns = new Object[count]; for (int i = 0; i < count; i++) switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_NULL: columns[i] = null; break; case Cursor.FIELD_TYPE_INTEGER: columns[i] = cursor.getInt(i); break; case Cursor.FIELD_TYPE_FLOAT: columns[i] = cursor.getFloat(i); break; case Cursor.FIELD_TYPE_STRING: columns[i] = cursor.getString(i); break; case Cursor.FIELD_TYPE_BLOB: columns[i] = cursor.getBlob(i); break; default: Util.log(this, Log.WARN, "Unknown cursor data type=" + cursor.getType(i)); } result.addRow(columns); } catch (Throwable ex) { Util.bug(this, ex); } }
Example 14
Source File: DatabaseResult.java From pandora with Apache License 2.0 | 5 votes |
private static List<List<String>> wrapRows(Cursor cursor) { List<List<String>> result = new ArrayList<>(); final int numColumns = cursor.getColumnCount(); while (cursor.moveToNext()) { ArrayList<String> flatList = new ArrayList<>(); 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; } } result.add(flatList); } return result; }
Example 15
Source File: DatabaseHelper.java From AndroidDemo with MIT License | 4 votes |
public static TableDataResponse getTableData(SQLiteDatabase db, String selectQuery, String tableName) { TableDataResponse tableData = new TableDataResponse(); tableData.isSelectQuery = true; if (tableName == null) { tableName = getTableName(selectQuery); } if (tableName != null) { final String pragmaQuery = "PRAGMA table_info(" + tableName + ")"; tableData.tableInfos = getTableInfo(db, pragmaQuery); } tableData.isEditable = tableName != null && tableData.tableInfos != null; Cursor cursor; try { cursor = db.rawQuery(selectQuery, null); } catch (Exception e) { e.printStackTrace(); tableData.isSuccessful = false; tableData.errorMessage = e.getMessage(); return tableData; } if (cursor != null) { cursor.moveToFirst(); // setting tableInfo when tableName is not known and making // it non-editable also by making isPrimary true for all if (tableData.tableInfos == null) { tableData.tableInfos = new ArrayList<>(); for (int i = 0; i < cursor.getColumnCount(); i++) { TableDataResponse.TableInfo tableInfo = new TableDataResponse.TableInfo(); tableInfo.title = cursor.getColumnName(i); tableInfo.isPrimary = true; tableData.tableInfos.add(tableInfo); } } tableData.isSuccessful = true; tableData.rows = new ArrayList<>(); if (cursor.getCount() > 0) { do { List<TableDataResponse.ColumnData> row = new ArrayList<>(); for (int i = 0; i < cursor.getColumnCount(); i++) { TableDataResponse.ColumnData columnData = new TableDataResponse.ColumnData(); switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_BLOB: columnData.dataType = DataType.TEXT; columnData.value = ConverterUtils.blobToString(cursor.getBlob(i)); break; case Cursor.FIELD_TYPE_FLOAT: columnData.dataType = DataType.REAL; columnData.value = cursor.getDouble(i); break; case Cursor.FIELD_TYPE_INTEGER: columnData.dataType = DataType.INTEGER; columnData.value = cursor.getLong(i); break; case Cursor.FIELD_TYPE_STRING: columnData.dataType = DataType.TEXT; columnData.value = cursor.getString(i); break; default: columnData.dataType = DataType.TEXT; columnData.value = cursor.getString(i); } row.add(columnData); } tableData.rows.add(row); } while (cursor.moveToNext()); } cursor.close(); return tableData; } else { tableData.isSuccessful = false; tableData.errorMessage = "Cursor is null"; return tableData; } }
Example 16
Source File: DatabaseHelper.java From AndroidDemo with MIT License | 4 votes |
public static TableDataResponse getTableData(SQLiteDatabase db, String selectQuery, String tableName) { TableDataResponse tableData = new TableDataResponse(); tableData.isSelectQuery = true; if (tableName == null) { tableName = getTableName(selectQuery); } if (tableName != null) { final String pragmaQuery = "PRAGMA table_info(" + tableName + ")"; tableData.tableInfos = getTableInfo(db, pragmaQuery); } tableData.isEditable = tableName != null && tableData.tableInfos != null; Cursor cursor; try { cursor = db.rawQuery(selectQuery, null); } catch (Exception e) { e.printStackTrace(); tableData.isSuccessful = false; tableData.errorMessage = e.getMessage(); return tableData; } if (cursor != null) { cursor.moveToFirst(); // setting tableInfo when tableName is not known and making // it non-editable also by making isPrimary true for all if (tableData.tableInfos == null) { tableData.tableInfos = new ArrayList<>(); for (int i = 0; i < cursor.getColumnCount(); i++) { TableDataResponse.TableInfo tableInfo = new TableDataResponse.TableInfo(); tableInfo.title = cursor.getColumnName(i); tableInfo.isPrimary = true; tableData.tableInfos.add(tableInfo); } } tableData.isSuccessful = true; tableData.rows = new ArrayList<>(); if (cursor.getCount() > 0) { do { List<TableDataResponse.ColumnData> row = new ArrayList<>(); for (int i = 0; i < cursor.getColumnCount(); i++) { TableDataResponse.ColumnData columnData = new TableDataResponse.ColumnData(); switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_BLOB: columnData.dataType = DataType.TEXT; columnData.value = ConverterUtils.blobToString(cursor.getBlob(i)); break; case Cursor.FIELD_TYPE_FLOAT: columnData.dataType = DataType.REAL; columnData.value = cursor.getDouble(i); break; case Cursor.FIELD_TYPE_INTEGER: columnData.dataType = DataType.INTEGER; columnData.value = cursor.getLong(i); break; case Cursor.FIELD_TYPE_STRING: columnData.dataType = DataType.TEXT; columnData.value = cursor.getString(i); break; default: columnData.dataType = DataType.TEXT; columnData.value = cursor.getString(i); } row.add(columnData); } tableData.rows.add(row); } while (cursor.moveToNext()); } cursor.close(); return tableData; } else { tableData.isSuccessful = false; tableData.errorMessage = "Cursor is null"; return tableData; } }
Example 17
Source File: HumanReadables.java From android-test with Apache License 2.0 | 4 votes |
public static String describe(Cursor c) { if (c.isBeforeFirst()) { return "Cursor positioned before first element."; } else if (c.isAfterLast()) { return "Cursor positioned after last element."; } StringBuilder result = new StringBuilder("Row ").append(c.getPosition()).append(": {"); String[] columns = c.getColumnNames(); for (int i = 0; i < columns.length; i++) { result.append(columns[i]).append(":"); int type = Cursor.FIELD_TYPE_STRING; if (Build.VERSION.SDK_INT > 10) { type = c.getType(i); } switch (type) { case Cursor.FIELD_TYPE_STRING: result.append("\"").append(c.getString(i)).append("\""); break; case Cursor.FIELD_TYPE_INTEGER: result.append(c.getLong(i)); break; case Cursor.FIELD_TYPE_FLOAT: result.append(c.getDouble(i)); result.append("f"); break; case Cursor.FIELD_TYPE_NULL: result.append("null"); break; case Cursor.FIELD_TYPE_BLOB: byte[] val = c.getBlob(i); result.append("["); for (int j = 0; j < 5 && j < val.length; j++) { result.append(val[j]); result.append(","); } if (5 < val.length) { result.append("... (").append(val.length - 5).append(" more elements)"); } result.append("]"); break; default: result.append("\"").append(c.getString(i)).append("\""); break; } result.append(", "); } result.append("}"); return result.toString(); }
Example 18
Source File: CursorHelper.java From libcommon with Apache License 2.0 | 4 votes |
/** * 指定したCursorの現在のレコードを文字列に変換 * @param cursor */ public static String toString(@Nullable final Cursor cursor) { if (cursor == null) { return "{null}"; } else if (cursor.isClosed()) { return "{closed}"; } else if (cursor.isBeforeFirst()) { return "{before first}"; } else if (cursor.isAfterLast()) { return "{after last}"; } else { final StringBuilder sb = new StringBuilder(); final int n = cursor.getColumnCount(); final String[] columnNames = cursor.getColumnNames(); sb.append("{"); for (int i = 0; i < n; i++) { switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_FLOAT: sb.append(columnNames[i]).append("=").append(cursor.getDouble(i)); break; case Cursor.FIELD_TYPE_INTEGER: sb.append(columnNames[i]).append("=").append(cursor.getLong(i)); break; case Cursor.FIELD_TYPE_STRING: sb.append(columnNames[i]).append("=").append(cursor.getString(i)); break; case Cursor.FIELD_TYPE_BLOB: sb.append(columnNames[i]).append("=").append("BLOB"); break; case Cursor.FIELD_TYPE_NULL: sb.append(columnNames[i]).append("=").append("NULL"); break; default: sb.append(columnNames[i]).append("=").append("UNKNOWN"); break; } if (i < n-1) { sb.append(","); } } sb.append("}"); return sb.toString(); } }
Example 19
Source File: Utils.java From fdroidclient with GNU General Public License v3.0 | 4 votes |
/** * Useful for debugging during development, so that arbitrary queries can be made, and their * results inspected in the debugger. */ @SuppressWarnings("unused") @RequiresApi(api = 11) public static List<Map<String, String>> dumpCursor(Cursor cursor) { List<Map<String, String>> data = new ArrayList<>(); if (cursor == null) { return data; } cursor.moveToFirst(); while (!cursor.isAfterLast()) { Map<String, String> row = new HashMap<>(cursor.getColumnCount()); for (String col : cursor.getColumnNames()) { int i = cursor.getColumnIndex(col); switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_NULL: row.put(col, null); break; case Cursor.FIELD_TYPE_INTEGER: row.put(col, Integer.toString(cursor.getInt(i))); break; case Cursor.FIELD_TYPE_FLOAT: row.put(col, Double.toString(cursor.getFloat(i))); break; case Cursor.FIELD_TYPE_STRING: row.put(col, cursor.getString(i)); break; case Cursor.FIELD_TYPE_BLOB: row.put(col, new String(cursor.getBlob(i), Charset.defaultCharset())); break; } } data.add(row); cursor.moveToNext(); } cursor.close(); return data; }
Example 20
Source File: DebugHelper.java From XMiTools with GNU General Public License v3.0 | 4 votes |
public static void printCursor(Uri uri, Cursor c) { XLog.d("%s", uri.toString()); if (c == null) { return; } boolean hasNext = c.moveToNext(); if (!hasNext) { return; } int columnCount = c.getColumnCount(); String[] columnNames = c.getColumnNames(); int[] columnTypes = new int[columnCount]; for (int i = 0; i < columnCount; i++) { columnTypes[i] = c.getType(i); } c.moveToPrevious(); while (c.moveToNext()) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < columnCount; i++) { Object value = null; int columnType = columnTypes[i]; if (columnType == Cursor.FIELD_TYPE_INTEGER) { c.getInt(i); } switch (columnType) { case Cursor.FIELD_TYPE_INTEGER: value = c.getInt(i); break; case Cursor.FIELD_TYPE_BLOB: value = c.getBlob(i); break; case Cursor.FIELD_TYPE_FLOAT: value = c.getFloat(i); break; case Cursor.FIELD_TYPE_STRING: value = c.getString(i); break; default: case Cursor.FIELD_TYPE_NULL: break; } sb.append(columnNames[i]).append(" = ").append(value).append(", "); } sb.append("\n"); XLog.d("%s", sb.toString()); } }