Example usage for android.database Cursor getColumnNames

List of usage examples for android.database Cursor getColumnNames

Introduction

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

Prototype

String[] getColumnNames();

Source Link

Document

Returns a string array holding the names of all of the columns in the result set in the order in which they were listed in the result.

Usage

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   ww w . j a  v  a2  s  .c  om
    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:org.mozilla.gecko.db.LocalURLMetadata.java

/**
 * Converts a Cursor into a unmodifiable Map of known metadata properties.
 * Will throw away any properties that aren't stored in the database.
 * Will also not iterate through multiple rows in the cursor.
 *///from   w w  w .  j a  va  2  s  . co m
private Map<String, Object> fromCursor(Cursor c) {
    Map<String, Object> data = new HashMap<String, Object>();

    Set<String> model = getModel();
    String[] columns = c.getColumnNames();
    for (String column : columns) {
        if (model.contains(column)) {
            try {
                data.put(column, c.getString(c.getColumnIndexOrThrow(column)));
            } catch (Exception ex) {
                Log.i(LOGTAG, "Error getting data for " + column, ex);
            }
        }
    }

    return Collections.unmodifiableMap(data);
}

From source file:io.nuclei.cyto.share.CytoFileProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor source = super.query(uri, projection, selection, selectionArgs, sortOrder);
    if (source == null)
        return null;
    try {/*from   w w w.  ja va 2 s. c o m*/
        final String[] columnNames = source.getColumnNames();

        String[] names = columnNames;
        List<String> missingNames = getMissingColumns(columnNames);
        int mimeTypeIndex = -1;

        if (missingNames.size() > 0) {
            String[] newColumnNames = Arrays.copyOf(columnNames, columnNames.length + missingNames.size());
            int ix = columnNames.length;
            for (String missingName : missingNames) {
                newColumnNames[ix] = missingName;
                if (MediaStore.MediaColumns.MIME_TYPE.equals(missingName))
                    mimeTypeIndex = ix;
                ix++;
            }
            names = newColumnNames;
        }

        MatrixCursor cursor = new MatrixCursor(names, source.getCount());
        source.moveToPosition(-1);
        int columnLength = columnNames.length;
        while (source.moveToNext()) {
            MatrixCursor.RowBuilder row = cursor.newRow();
            for (int i = 0; i < names.length; i++) {
                if (i < columnLength)
                    row.add(source.getString(i));
                else if (i == mimeTypeIndex) {
                    // populate the MIME_TYPE column with a guess as to the mime type of the file
                    final int lastDot = uri.getPath().lastIndexOf('.');
                    if (lastDot >= 0) {
                        String extension = uri.getPath().substring(lastDot + 1);
                        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                        if (mimeType != null)
                            row.add(mimeType);
                        else
                            row.add("application/octet");
                    } else
                        row.add("application/octet");
                } else
                    row.add(null);
            }
        }

        return cursor;
    } finally {
        source.close();
    }
}

From source file:android.database.DatabaseUtils.java

/**
 * Read the entire contents of a cursor row and store them in a ContentValues.
 *
 * @param cursor the cursor to read from.
 * @param values the {@link ContentValues} to put the row into.
 *//*from  w ww. java 2  s. co  m*/
public static void cursorRowToContentValues(Cursor cursor, ContentValues values) {
    AbstractWindowedCursor awc = (cursor instanceof AbstractWindowedCursor) ? (AbstractWindowedCursor) cursor
            : null;

    String[] columns = cursor.getColumnNames();
    int length = columns.length;
    for (int i = 0; i < length; i++) {
        if (awc != null && awc.isBlob(i)) {
            values.put(columns[i], cursor.getBlob(i));
        } else {
            values.put(columns[i], cursor.getString(i));
        }
    }
}

From source file:com.googlecode.android_scripting.facade.SmsFacade.java

@Rpc(description = "Returns a List of all possible message attributes.")
public List<String> smsGetAttributes() {
    List<String> result = new ArrayList<String>();
    Cursor cursor = mContentResolver.query(Uri.parse("content://sms"), null, null, null, null);
    if (cursor != null) {
        String[] columns = cursor.getColumnNames();
        for (int i = 0; i < columns.length; i++) {
            result.add(columns[i]);/*from  w  ww .j av a 2  s  . c  o  m*/
        }
        cursor.close();
    } else {
        result = null;
    }
    return result;
}

From source file:com.dabay6.android.apps.carlog.ui.statistics.fragments.AveragesFragment.java

/**
 * {@inheritDoc}/*  ww w  .  j a  v a2 s.c  o m*/
 */
@Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
    final DetailsAdapter adapter;
    final DetailsItemList items = new DetailsItemList();

    if (DataUtils.hasData(cursor)) {
        final String[] columnNames = cursor.getColumnNames();

        for (final String columnName : columnNames) {
            if (!columnName.equalsIgnoreCase(Columns.VEHICLE_ID.getName())) {
                final DetailsItem item;
                final int index = cursor.getColumnIndex(columnName);
                final String text;
                final float value = cursor.getFloat(index);

                if (columnName.equalsIgnoreCase(Columns.AVERAGE_FUEL_AMOUNT.getName())
                        || columnName.equalsIgnoreCase(Columns.AVERAGE_MILES_PER_GALLON.getName())
                        || columnName.equalsIgnoreCase(Columns.MAX_MILES_PER_GALLON.getName())) {
                    text = String.format(getString(R.string.gallons), value);
                } else {
                    final NumberFormat currency = NumberFormat.getCurrencyInstance();

                    text = currency.format(cursor.getFloat(index));
                }

                item = new DetailsItem(StringUtils.splitIntoWords(columnName), text);

                items.add(item);
            }
        }
    }

    adapter = new DetailsAdapter(getActivity(), items, layout.list_item_statistics);

    setListAdapter(adapter);

    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

From source file:com.dabay6.android.apps.carlog.ui.statistics.fragments.TotalsFragment.java

/**
 * {@inheritDoc}//from  www .  j a va  2s.com
 */
@Override
public void onLoadFinished(final Loader<Cursor> cursorLoader, final Cursor cursor) {
    final DetailsAdapter adapter;
    final DetailsItemList items = new DetailsItemList();

    if (DataUtils.hasData(cursor)) {
        final String[] columnNames = cursor.getColumnNames();

        for (final String columnName : columnNames) {
            if (!columnName.equalsIgnoreCase(Columns.VEHICLE_ID.getName())) {
                final DetailsItem item;
                final int index = cursor.getColumnIndex(columnName);
                final String text;
                final float value = cursor.getFloat(index);

                if (columnName.equalsIgnoreCase(Columns.TOTAL_DISTANCE.getName())) {
                    text = String.format(getString(R.string.fuel_history_miles), value);
                } else if (columnName.equalsIgnoreCase(Columns.TOTAL_FUEL.getName())) {
                    text = String.format(getString(R.string.gallons), value);
                } else {
                    final NumberFormat currency = NumberFormat.getCurrencyInstance();

                    text = currency.format(cursor.getFloat(index));
                }

                item = new DetailsItem(StringUtils.splitIntoWords(columnName), text);

                items.add(item);
            }
        }
    }

    adapter = new DetailsAdapter(getActivity(), items, layout.list_item_statistics);

    setListAdapter(adapter);

    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

From source file:com.dabay6.android.apps.carlog.ui.statistics.fragments.FillUpsFragment.java

/**
 * {@inheritDoc}//from   w  w w  .ja v a2  s . c  om
 */
@Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
    final DetailsAdapter adapter;
    final DetailsItemList items = new DetailsItemList();

    if (DataUtils.hasData(cursor)) {
        final String[] columnNames = cursor.getColumnNames();

        for (final String columnName : columnNames) {
            if (!columnName.equalsIgnoreCase(Columns.VEHICLE_ID.getName())) {
                final DetailsItem item;
                final int index = cursor.getColumnIndex(columnName);
                final String text;
                final float value = cursor.getFloat(index);

                if (columnName.equalsIgnoreCase(Columns.TOTAL_FILL_UPS.getName())) {
                    text = StringUtils.format("%.0f", value);
                } else if (columnName.equalsIgnoreCase(Columns.MAX_FUEL_AMOUNT.getName())
                        || columnName.equalsIgnoreCase(Columns.MIN_FUEL_AMOUNT.getName())) {
                    text = StringUtils.format(getString(R.string.gallons), value);
                } else {
                    final NumberFormat currency = NumberFormat.getCurrencyInstance();

                    text = currency.format(cursor.getFloat(index));
                }

                item = new DetailsItem(StringUtils.splitIntoWords(columnName), text);

                items.add(item);
            }
        }
    }

    adapter = new DetailsAdapter(getActivity(), items, layout.list_item_statistics);

    setListAdapter(adapter);

    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

From source file:com.googlecode.android_scripting.facade.ContactsFacade.java

@Rpc(description = "Returns a List of all possible attributes for contacts.")
public List<String> contactsGetAttributes() {
    List<String> result = new ArrayList<String>();
    Cursor cursor = mContentResolver.query(CONTACTS_URI, null, null, null, null);
    if (cursor != null) {
        String[] columns = cursor.getColumnNames();
        for (int i = 0; i < columns.length; i++) {
            result.add(columns[i]);/*from  www .ja  va2 s  .com*/
        }
        cursor.close();
    }
    return result;
}

From source file:com.googlecode.android_scripting.facade.ContactsFacade.java

@Rpc(description = "Content Resolver Query Attributes", returns = "a list of available columns for a given content uri")
public JSONArray queryAttributes(
        @RpcParameter(name = "uri", description = "The URI, using the content:// scheme, for the content to retrieve.") String uri)
        throws JSONException {
    JSONArray result = new JSONArray();
    Cursor cursor = mContentResolver.query(Uri.parse(uri), null, "1=0", null, null);
    if (cursor != null) {
        String[] names = cursor.getColumnNames();
        for (String name : names) {
            result.put(name);/*from  w ww . j a va 2 s  .c om*/
        }
        cursor.close();
    }
    return result;
}