Java Code Examples for android.database.Cursor#FIELD_TYPE_STRING

The following examples show how to use android.database.Cursor#FIELD_TYPE_STRING . 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: DBProxy.java    From android-orm with Apache License 2.0 6 votes vote down vote up
/**
 * 封装内容到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 2
Source File: PermissionTester.java    From PermissionAgent with Apache License 2.0 6 votes vote down vote up
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 3
Source File: SqliteStorageManager.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte convertLocalTypeToGSN(int jdbcType, int precision) {
	switch (jdbcType) {
		case Cursor.FIELD_TYPE_INTEGER:
			return DataTypes.INTEGER;
		case Cursor.FIELD_TYPE_STRING:
			return DataTypes.VARCHAR;
		case Cursor.FIELD_TYPE_FLOAT:
			return DataTypes.DOUBLE;
		case Cursor.FIELD_TYPE_BLOB:
			return DataTypes.BINARY;
		default:
			Log.e(TAG, "convertLocalTypeToGSN: The type can't be converted to GSN form : " + jdbcType);
			break;
	}
	return -100;
}
 
Example 4
Source File: XContentResolver.java    From XPrivacy with GNU General Public License v3.0 6 votes vote down vote up
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 5
Source File: OCursorListAdapter.java    From hr with GNU Affero General Public License v3.0 6 votes vote down vote up
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 6
Source File: XContentResolver.java    From XPrivacy with GNU General Public License v3.0 5 votes vote down vote up
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 7
Source File: LegacyCompatCursorWrapper.java    From cwac-provider with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getType(int columnIndex) {
  if (!cursorHasDataColumn() && columnIndex==fakeDataColumn) {
    return(Cursor.FIELD_TYPE_STRING);
  }

  if (!cursorHasMimeTypeColumn() && columnIndex==fakeMimeTypeColumn) {
    return(Cursor.FIELD_TYPE_STRING);
  }

  return(super.getType(columnIndex));
}
 
Example 8
Source File: PaginatedCursor.java    From BuildingForAndroidTV with MIT License 5 votes vote down vote up
/**
 * Try to load un-cached data with size {@link PAGE_SIZE} starting from given index.
 */
private void loadCacheStartingFromPosition(int index) {
    mCursor.moveToPosition(index);
    for (int row = index; row < (index + PAGE_SIZE) && row < mRowCount; row++) {
        if (!mCachedRows[row]) {
            for (int col = 0; col < mColumnCount; col++) {
                switch (mCursor.getType(col)) {
                    case Cursor.FIELD_TYPE_BLOB:
                        mByteArrayDataCache[row][mByteArrayCacheIndexMap[col]] =
                                mCursor.getBlob(col);
                        break;
                    case Cursor.FIELD_TYPE_FLOAT:
                        mFloatDataCache[row][mFloatCacheIndexMap[col]] = mCursor.getFloat(col);
                        break;
                    case Cursor.FIELD_TYPE_INTEGER:
                        mIntDataCache[row][mIntCacheIndexMap[col]] = mCursor.getInt(col);
                        break;
                    case Cursor.FIELD_TYPE_STRING:
                        mStringDataCache[row][mStringCacheIndexMap[col]] =
                                mCursor.getString(col);
                        break;
                }
            }
            mCachedRows[row] = true;
        }
        mCursor.moveToNext();
    }
    mLastCachePosition = Math.min(index + PAGE_SIZE, mRowCount) - 1;
}
 
Example 9
Source File: DataBaseUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static int getTypeOfObject(Object obj) {
    if (obj == null) {
        return Cursor.FIELD_TYPE_NULL;
    } else if (obj instanceof byte[]) {
        return Cursor.FIELD_TYPE_BLOB;
    } else if (obj instanceof Float || obj instanceof Double) {
        return Cursor.FIELD_TYPE_FLOAT;
    } else if (obj instanceof Long || obj instanceof Integer
            || obj instanceof Short || obj instanceof Byte) {
        return Cursor.FIELD_TYPE_INTEGER;
    } else {
        return Cursor.FIELD_TYPE_STRING;
    }
}
 
Example 10
Source File: DatabaseHelper.java    From AndroidDemo with MIT License 4 votes vote down vote up
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 11
Source File: Utils.java    From easyDAO with Apache License 2.0 4 votes vote down vote up
public static <T extends BaseEntity> List<T> cursor2Entity(Class<T> clazz, Cursor cursor) throws DBException {
    List<T> objList = new ArrayList<>();
    Field[] fields = getDeclaredField(clazz);
    try {
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                T obj = clazz.newInstance();

                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    String strColName = cursor.getColumnName(i);
                    for (Field field : fields) {
                        if (field.getName().equals(strColName)) {
                            strColName = toUpperCaseFirstOne(strColName);
                            if (cursor.getType(i) == Cursor.FIELD_TYPE_NULL) {
                                continue;
                            } else if (cursor.getType(i) == Cursor.FIELD_TYPE_FLOAT) {
                                clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                        cursor.getFloat(i));
                            } else if (cursor.getType(i) == Cursor.FIELD_TYPE_INTEGER) {
                                if (field.getGenericType().toString().equals("class java.lang.Boolean")
                                        || field.getGenericType().toString().equals("boolean")) {
                                    // e.g. boolean isOk; public boolean isOk(){ return isOk; }   public void setOk(){}
                                    clazz.getMethod("set" + strColName.replaceFirst("Is", ""), field.getType()).invoke(obj,
                                            cursor.getInt(i) == 1 ? true : false);
                                } else if (field.getGenericType().toString().equals("class java.lang.Integer")
                                        || field.getGenericType().toString().equals("int")) {
                                    clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                            cursor.getInt(i));
                                } else if (field.getGenericType().toString().equals("class java.lang.Long")
                                        || field.getGenericType().toString().equals("long")) {
                                    clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                            (long) cursor.getInt(i));
                                } else if (field.getGenericType().toString().equals("class java.lang.Short")
                                        || field.getGenericType().toString().equals("short")) {
                                    clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                            (short) cursor.getInt(i));
                                } else if (field.getGenericType().toString().equals("class java.lang.Byte")
                                        || field.getGenericType().toString().equals("byte")) {
                                    clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                            (byte) cursor.getInt(i));
                                }
                            } else if (cursor.getType(i) == Cursor.FIELD_TYPE_STRING) {
                                clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                        cursor.getString(i));
                            } else if (cursor.getType(i) == Cursor.FIELD_TYPE_BLOB) {
                                clazz.getMethod("set" + strColName, field.getType()).invoke(obj,
                                        cursor.getBlob(i));
                            } else {
                                throw new DBException(null);
                            }
                            break;
                        }
                    }
                }
                objList.add(obj);
                cursor.moveToNext();
            }
            return objList;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new DBException(null);
    }
    return objList;
}
 
Example 12
Source File: SQLiteConnection.java    From sqlite-android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns data type of the given object's value.
 *<p>
 * Returned values are
 * <ul>
 *   <li>{@link Cursor#FIELD_TYPE_NULL}</li>
 *   <li>{@link Cursor#FIELD_TYPE_INTEGER}</li>
 *   <li>{@link Cursor#FIELD_TYPE_FLOAT}</li>
 *   <li>{@link Cursor#FIELD_TYPE_STRING}</li>
 *   <li>{@link Cursor#FIELD_TYPE_BLOB}</li>
 *</ul>
 *</p>
 *
 * @param obj the object whose value type is to be returned
 * @return object value type
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static int getTypeOfObject(Object obj) {
    if (obj == null) {
        return Cursor.FIELD_TYPE_NULL;
    } else if (obj instanceof byte[]) {
        return Cursor.FIELD_TYPE_BLOB;
    } else if (obj instanceof Float || obj instanceof Double) {
        return Cursor.FIELD_TYPE_FLOAT;
    } else if (obj instanceof Long || obj instanceof Integer
        || obj instanceof Short || obj instanceof Byte) {
        return Cursor.FIELD_TYPE_INTEGER;
    } else {
        return Cursor.FIELD_TYPE_STRING;
    }
}
 
Example 13
Source File: MainActivity.java    From cwac-provider with Apache License 2.0 4 votes vote down vote up
private void populateCursorTable() {
  Cursor c=getContentResolver().query(getUri(), PROJECTION,
    null, null, null);
  LayoutInflater inflater=getLayoutInflater();

  c.moveToFirst();
  cursorTable.removeAllViews();

  for (int i=0;i<c.getColumnCount();i++) {
    View row=inflater.inflate(R.layout.row, cursorTable, false);
    TextView name=(TextView)row.findViewById(R.id.name);
    TextView value=(TextView)row.findViewById(R.id.value);

    name.setText(c.getColumnName(i));

    switch (c.getType(i)) {
      case Cursor.FIELD_TYPE_STRING:
        String text=c.getString(i);

        if (text==null) {
          value.setText("null");
          value.setTypeface(null, Typeface.ITALIC);
        }
        else {
          value.setText(text);
        }

        break;

      case Cursor.FIELD_TYPE_INTEGER:
        value.setText(Integer.toString(c.getInt(i)));
        break;

      default:
        value.setText("**UNEXPECTED TYPE**: "+
          Integer.toString(c.getType(i)));
        value.setTypeface(null, Typeface.ITALIC);
        break;
    }

    cursorTable.addView(row);
  }
}
 
Example 14
Source File: DatabaseHelper.java    From AndroidDemo with MIT License 4 votes vote down vote up
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 15
Source File: SqliteManagerPresenter.java    From SqliteManager with Apache License 2.0 4 votes vote down vote up
private
@NonNull
SqliteResponseData getSqliteResponseDataFromQuery(String query, String[] selectionArgs) {
    SqliteResponse sqliteResponse = mSqliteResponseRetriever.getData(query, selectionArgs);
    if (sqliteResponse.isQuerySuccess()) {
        try {
            Cursor cursor = sqliteResponse.getCursor();
            String[] selectedTableColumnNames = cursor.getColumnNames();
            int[] selectedTableColumnTypes = new int[selectedTableColumnNames.length];
            int columnCount = cursor.getColumnCount();
            List<SparseArray<Object>> valuesArray = new ArrayList<>(cursor.getCount());

            if (cursor.moveToFirst()) {
                do {
                    SparseArray<Object> columnValuePair = new SparseArray<>(columnCount);
                    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
                        int fieldType = cursor.getType(columnIndex);
                        selectedTableColumnTypes[columnIndex] = fieldType;
                        if (fieldType == Cursor.FIELD_TYPE_NULL) {
                            columnValuePair.put(columnIndex, cursor.getString(columnIndex));
                        } else if (fieldType == Cursor.FIELD_TYPE_INTEGER) {
                            columnValuePair.put(columnIndex, cursor.getInt(columnIndex));
                        } else if (fieldType == Cursor.FIELD_TYPE_FLOAT) {
                            columnValuePair.put(columnIndex, cursor.getFloat(columnIndex));
                        } else if (fieldType == Cursor.FIELD_TYPE_STRING) {
                            columnValuePair.put(columnIndex, cursor.getString(columnIndex));
                        } else if (fieldType == Cursor.FIELD_TYPE_BLOB) {
                            columnValuePair.put(columnIndex, cursor.getBlob(columnIndex));
                        } else {
                            // never in this case
                            columnValuePair.put(columnIndex, cursor.getString(columnIndex));
                        }
                    }
                    valuesArray.add(columnValuePair);
                } while (cursor.moveToNext());
            }
            return new SqliteResponseData(columnCount, selectedTableColumnNames, selectedTableColumnTypes, valuesArray);
        } catch (Exception exception) {
            // sometimes cursor will not be null. when there are any constraints
            return new SqliteResponseData(exception);
        } finally {
            mSqliteDataRetriever.freeResources();
        }
    } else {
        mSqliteDataRetriever.freeResources();
        return new SqliteResponseData(sqliteResponse.getThrowable());
    }
}
 
Example 16
Source File: ChromeFileProvider.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@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);
    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;
}
 
Example 17
Source File: Utils.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 18
Source File: AudioUtil.java    From RhymeMusic with Apache License 2.0 4 votes vote down vote up
/**
 * 返回一个带有音乐信息的List
 * @param context 环境参数
 * @return 返回一个带有音乐信息的List
 */
public static ArrayList<Audio> getAudioList(Context context)
{
    ArrayList<Audio> audioList = new ArrayList<Audio>();
    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            AUDIO_KEYS, null, null, null);

    for ( cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext() )
    {
        Bundle bundle = new Bundle();

        for ( int i = 0; i < AUDIO_KEYS.length; i++ )
        {
            final String key = AUDIO_KEYS[i];
            final int colIndex = cursor.getColumnIndex(key);
            final int type = cursor.getType(colIndex);

            switch (type)
            {
                case Cursor.FIELD_TYPE_BLOB:
                    break;

                case Cursor.FIELD_TYPE_FLOAT:
                    float fValue = cursor.getFloat(colIndex);
                    bundle.putFloat(key, fValue);
                    break;

                case Cursor.FIELD_TYPE_INTEGER:
                    int iValue = cursor.getInt(colIndex);
                    bundle.putInt(key, iValue);
                    break;

                case Cursor.FIELD_TYPE_NULL:
                    break;

                case Cursor.FIELD_TYPE_STRING:
                    String sValue = cursor.getString(colIndex);
                    bundle.putString(key, sValue);
                    break;
            }
        }

        Audio audio = new Audio(bundle);
        audioList.add(audio);
    }

    cursor.close();
    return audioList;
}
 
Example 19
Source File: SQLiteConnection.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
    final int count = bindArgs != null ? bindArgs.length : 0;
    if (count != statement.mNumParameters) {
        throw new SQLiteBindOrColumnIndexOutOfRangeException(
                "Expected " + statement.mNumParameters + " bind arguments but "
                + count + " were provided.");
    }
    if (count == 0) {
        return;
    }

    final long statementPtr = statement.mStatementPtr;
    for (int i = 0; i < count; i++) {
        final Object arg = bindArgs[i];
        switch (DatabaseUtils.getTypeOfObject(arg)) {
            case Cursor.FIELD_TYPE_NULL:
                nativeBindNull(mConnectionPtr, statementPtr, i + 1);
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                nativeBindLong(mConnectionPtr, statementPtr, i + 1,
                        ((Number)arg).longValue());
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                nativeBindDouble(mConnectionPtr, statementPtr, i + 1,
                        ((Number)arg).doubleValue());
                break;
            case Cursor.FIELD_TYPE_BLOB:
                nativeBindBlob(mConnectionPtr, statementPtr, i + 1, (byte[])arg);
                break;
            case Cursor.FIELD_TYPE_STRING:
            default:
                if (arg instanceof Boolean) {
                    // Provide compatibility with legacy applications which may pass
                    // Boolean values in bind args.
                    nativeBindLong(mConnectionPtr, statementPtr, i + 1,
                            ((Boolean)arg).booleanValue() ? 1 : 0);
                } else {
                    nativeBindString(mConnectionPtr, statementPtr, i + 1, arg.toString());
                }
                break;
        }
    }
}
 
Example 20
Source File: DebugHelper.java    From XMiTools with GNU General Public License v3.0 4 votes vote down vote up
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());
    }
}