Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

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

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.

Usage

From source file:cc.solart.turbo.BaseCursorAdapter.java

/**
 * Constructor that disallows control over auto-requery. As an alternative,
 * use {@link android.app.LoaderManager}/{@link android.support.v4.app.LoaderManager}
 * with a {@link android.content.CursorLoader}/{@link android.support.v4.content.CursorLoader}
 *
 * @param c       The cursor from which to get the data.
 * @param context The context// ww  w  .  j  av a 2  s. c  om
 */
public BaseCursorAdapter(Context context, Cursor c) {
    super(context);
    boolean cursorPresent = c != null;
    mCursor = c;
    mDataValid = cursorPresent;
    mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

private List<Todo> getTodosWithSelection(ContentResolver contentResolver, String selection) {
    Cursor cursor = contentResolver.query(TodoContentProvider.CONTENT_URI, null, selection, null, null);

    List<Todo> list = new ArrayList<Todo>();

    while (cursor.moveToNext()) {
        int localId = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_ID));
        int serverId = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_SERVER_ID));
        String name = cursor.getString(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_TITLE));
        int status = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_STATUS_FLAG));

        Todo currentTodo = new Todo();

        if (status == StatusFlag.ADD) {
            currentTodo.setId((long) localId);
        } else {/*from   w w  w  .j a v  a  2s .co  m*/
            currentTodo.setId((long) serverId);
        }
        currentTodo.setTitle(name);
        currentTodo.setStatus(status);

        list.add(currentTodo);
    }

    cursor.close();
    return list;
}

From source file:com.amaze.filemanager.utils.files.FileUtils.java

private static Uri fileToContentUri(Context context, String path, boolean isDirectory, String volume) {
    final String where = MediaStore.MediaColumns.DATA + " = ?";
    Uri baseUri;//from   w  w w. ja v  a2s .  co m
    String[] projection;
    int mimeType = Icons.getTypeOfFile(path, isDirectory);

    switch (mimeType) {
    case Icons.IMAGE:
        baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        projection = new String[] { BaseColumns._ID };
        break;
    case Icons.VIDEO:
        baseUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        projection = new String[] { BaseColumns._ID };
        break;
    case Icons.AUDIO:
        baseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        projection = new String[] { BaseColumns._ID };
        break;
    default:
        baseUri = MediaStore.Files.getContentUri(volume);
        projection = new String[] { BaseColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE };
    }

    ContentResolver cr = context.getContentResolver();
    Cursor c = cr.query(baseUri, projection, where, new String[] { path }, null);
    try {
        if (c != null && c.moveToNext()) {
            boolean isValid = false;
            if (mimeType == Icons.IMAGE || mimeType == Icons.VIDEO || mimeType == Icons.AUDIO) {
                isValid = true;
            } else {
                int type = c.getInt(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE));
                isValid = type != 0;
            }

            if (isValid) {
                // Do not force to use content uri for no media files
                long id = c.getLong(c.getColumnIndexOrThrow(BaseColumns._ID));
                return Uri.withAppendedPath(baseUri, String.valueOf(id));
            }
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

public Todo isTodoInDb(ContentResolver contentResolver, Long serverId) {
    Todo todo = null;/*from  ww w  . ja  va  2s .  c o  m*/

    Cursor cursor = contentResolver.query(TodoContentProvider.CONTENT_URI, null,
            TodoContentProvider.COLUMN_SERVER_ID + "=" + serverId, null, null);

    if (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_TITLE));
        int status = cursor.getInt(cursor.getColumnIndexOrThrow(TodoContentProvider.COLUMN_STATUS_FLAG));

        todo = new Todo();
        todo.setId(serverId);
        todo.setTitle(name);
        todo.setStatus(status);
    }

    cursor.close();
    return todo;
}

From source file:com.android.providers.contacts.ContactsSyncAdapter.java

private static void addExtensionsToContactEntry(ContentResolver cr, long personId, ContactEntry entry)
        throws ParseException {
    Cursor c = cr.query(Extensions.CONTENT_URI, null, "person=" + personId, null, null);
    try {//from   w  w  w .  j  a v a2  s  .  c om
        JSONObject jsonObject = new JSONObject();
        int nameIndex = c.getColumnIndexOrThrow(Extensions.NAME);
        int valueIndex = c.getColumnIndexOrThrow(Extensions.VALUE);
        if (c.getCount() == 0)
            return;
        while (c.moveToNext()) {
            try {
                jsonObject.put(c.getString(nameIndex), c.getString(valueIndex));
            } catch (JSONException e) {
                throw new ParseException("bad key or value", e);
            }
        }
        ExtendedProperty extendedProperty = new ExtendedProperty();
        extendedProperty.setName("android");
        final String jsonString = jsonObject.toString();
        if (jsonString == null) {
            throw new ParseException(
                    "unable to convert cursor into a JSON string, " + DatabaseUtils.dumpCursorToString(c));
        }
        extendedProperty.setXmlBlob(jsonString);
        entry.addExtendedProperty(extendedProperty);
    } finally {
        if (c != null)
            c.close();
    }
}

From source file:com.crust87.videotrackviewsample.MainActivity.java

private String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;

    try {/*from w w  w.j av a2s  .  co  m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = mActivity.getContentResolver().query(contentUri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(columnIndex);
        } else {
            return null;
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

public int getTodoStatus(ContentResolver contentResolver, Long id) {
    Cursor c = contentResolver.query(TodoContentProvider.CONTENT_URI, null,
            TodoContentProvider.COLUMN_ID + "=" + id, null, null);

    int status = 0;

    try {/*from w  ww.  ja  v a 2 s  . co  m*/
        if (c.moveToNext()) {
            status = c.getInt(c.getColumnIndexOrThrow(TodoContentProvider.COLUMN_STATUS_FLAG));

        } else {
            throw new RuntimeException("Tried to delete a non existent todo");
        }
    } finally {
        c.close();
    }

    return status;
}

From source file:edu.stanford.mobisocial.dungbeetle.DBIdentityProvider.java

public String userProfile() {
    Cursor c = mHelper.getReadableDatabase().rawQuery("SELECT * FROM " + MyInfo.TABLE, new String[] {});

    try {//from  ww w .  j  a  v a  2 s .c  om
        c.moveToFirst();
        JSONObject obj = new JSONObject();
        try {
            obj.put("name", c.getString(c.getColumnIndexOrThrow(MyInfo.NAME)));
        } catch (JSONException e) {
        }
        return JSON.fastAddBase64(obj.toString(), "picture",
                c.getBlob(c.getColumnIndexOrThrow(MyInfo.PICTURE)));
    } finally {
        c.close();
    }
}

From source file:com.crust87.ffmpegexecutorsample.MainActivity.java

public String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;
    try {// www .j av  a  2  s .com
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.feathercoin.wallet.feathercoin.ui.SendingAddressesFragment.java

@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
    activity.startActionMode(new ActionMode.Callback() {
        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.sending_addresses_context, menu);

            return true;
        }/* w  w  w  .  java 2  s  .  com*/

        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            final String label = getLabel(position);
            mode.setTitle(label);

            return true;
        }

        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.sending_addresses_context_send:
                handleSend(getAddress(position));

                mode.finish();
                return true;

            case R.id.sending_addresses_context_edit:
                EditAddressBookEntryFragment.edit(getFragmentManager(), getAddress(position));

                mode.finish();
                return true;

            case R.id.sending_addresses_context_remove:
                handleRemove(getAddress(position));

                mode.finish();
                return true;

            case R.id.sending_addresses_context_show_qr:
                handleShowQr(getAddress(position));

                mode.finish();
                return true;

            case R.id.sending_addresses_context_copy_to_clipboard:
                handleCopyToClipboard(getAddress(position));

                mode.finish();
                return true;
            }

            return false;
        }

        public void onDestroyActionMode(final ActionMode mode) {
        }

        private String getAddress(final int position) {
            final Cursor cursor = (Cursor) adapter.getItem(position);
            return cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS));
        }

        private String getLabel(final int position) {
            final Cursor cursor = (Cursor) adapter.getItem(position);
            return cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_LABEL));
        }
    });
}