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:org.mythtv.service.dvr.v26.ProgramHelperV26.java

public void processProgram(final Context context, final LocationProfile locationProfile, Uri uri, String table,
        ArrayList<ContentProviderOperation> ops, Program program, String tag) {
    //      Log.d( TAG, "processProgram : enter" );

    ContentValues programValues = convertProgramToContentValues(locationProfile, program, tag);

    if (table.equals(ProgramConstants.TABLE_NAME_RECORDED) || table.equals(ProgramConstants.TABLE_NAME_UPCOMING)
            || table.equals(ProgramConstants.TABLE_NAME_GUIDE)) {

        //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannelInfo().getChannelId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostname() + ":" + table );
        ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true)
                .build());//from ww w.  j a v  a 2s  . c om

    } else {

        String programSelection = table + "." + ProgramConstants.FIELD_CHANNEL_ID + " = ? AND " + table + "."
                + ProgramConstants.FIELD_START_TIME + " = ?";
        String[] programSelectionArgs = new String[] { String.valueOf(program.getChannel().getChanId()),
                String.valueOf(program.getStartTime().getMillis()) };

        programSelection = appendLocationHostname(context, locationProfile, programSelection, table);

        Cursor programCursor = context.getContentResolver().query(uri, programProjection, programSelection,
                programSelectionArgs, null);
        if (programCursor.moveToFirst()) {
            Log.v(TAG,
                    "processProgram : UPDATE PROGRAM : " + program.getTitle() + ":" + program.getSubTitle()
                            + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":"
                            + program.getEndTime() + ":" + program.getHostName() + ":" + table);

            Long id = programCursor.getLong(programCursor.getColumnIndexOrThrow(ProgramConstants._ID));
            ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(uri, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {
            //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table );

            ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true)
                    .build());

        }
        programCursor.close();

    }

    //      Log.d( TAG, "processProgram : exit" );
}

From source file:com.gsma.rcs.ri.messaging.OneToOneTalkView.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_1to1_talk_item, menu);
    menu.findItem(R.id.menu_resend_message).setVisible(false);
    menu.findItem(R.id.menu_display_content).setVisible(false);
    menu.findItem(R.id.menu_listen_content).setVisible(false);
    /* Get the list item position */
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    Cursor cursor = (Cursor) mAdapter.getItem(info.position);
    /* Adapt the contextual menu according to the selected item */
    int providerId = cursor.getInt(cursor.getColumnIndexOrThrow(HistoryLog.PROVIDER_ID));
    String id = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.ID));
    Direction direction = Direction.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(HistoryLog.DIRECTION)));
    try {/* ww w .  j a v a  2s  .c om*/
        switch (providerId) {
        case ChatLog.Message.HISTORYLOG_MEMBER_ID:
            if (Direction.OUTGOING == direction) {
                ChatLog.Message.Content.Status status = ChatLog.Message.Content.Status
                        .valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(HistoryLog.STATUS)));
                if (ChatLog.Message.Content.Status.FAILED == status) {
                    String number = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.CONTACT));
                    if (number != null) {
                        ContactId contact = ContactUtil.formatContact(number);
                        OneToOneChat chat = mChatService.getOneToOneChat(contact);
                        if (chat != null && chat.isAllowedToSendMessage()) {
                            menu.findItem(R.id.menu_resend_message).setVisible(true);
                        }
                    }
                }
            }
            break;

        case FileTransferLog.HISTORYLOG_MEMBER_ID:
            String mimeType = cursor.getString(cursor.getColumnIndexOrThrow(HistoryLog.MIME_TYPE));
            FileTransfer.State state = FileTransfer.State
                    .valueOf(cursor.getInt(cursor.getColumnIndexOrThrow(HistoryLog.STATUS)));
            if (FileTransfer.State.FAILED == state) {
                FileTransfer transfer = mFileTransferService.getFileTransfer(id);
                if (transfer != null && transfer.isAllowedToResendTransfer()) {
                    menu.findItem(R.id.menu_resend_message).setVisible(true);
                }
            } else if (Utils.isImageType(mimeType)) {
                if (Direction.OUTGOING == direction) {
                    menu.findItem(R.id.menu_display_content).setVisible(true);

                } else if (Direction.INCOMING == direction) {
                    Long transferred = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.TRANSFERRED));
                    Long size = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.FILESIZE));
                    if (size.equals(transferred)) {
                        menu.findItem(R.id.menu_display_content).setVisible(true);
                    }
                }
            } else if (Utils.isAudioType(mimeType)) {
                if (Direction.OUTGOING == direction) {
                    menu.findItem(R.id.menu_listen_content).setVisible(true);

                } else if (Direction.INCOMING == direction) {
                    Long transferred = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.TRANSFERRED));
                    Long size = cursor.getLong(cursor.getColumnIndexOrThrow(HistoryLog.FILESIZE));
                    if (size.equals(transferred)) {
                        menu.findItem(R.id.menu_listen_content).setVisible(true);
                    }
                }
            }
            break;

        default:
            throw new IllegalArgumentException("Invalid provider ID=" + providerId);
        }
    } catch (RcsServiceNotAvailableException e) {
        menu.findItem(R.id.menu_resend_message).setVisible(false);

    } catch (RcsGenericException | RcsPersistentStorageException e) {
        menu.findItem(R.id.menu_resend_message).setVisible(false);
        showException(e);
    }
}

From source file:com.eleybourn.bookcatalogue.BookEdit.java

/**
 * Load a genre list; reloading this list every time a tab changes is slow.
 * So we cache it.//from ww  w  .j a v  a 2 s  .co m
 * 
 * @return List of publishers
 */
public ArrayList<String> getGenres() {
    if (mGenres == null) {
        mGenres = new ArrayList<String>();
        Cursor genre_cur = mDbHelper.fetchAllGenres("");
        final int col = genre_cur.getColumnIndexOrThrow(CatalogueDBAdapter.KEY_ROWID);
        try {
            while (genre_cur.moveToNext()) {
                mGenres.add(genre_cur.getString(col));
            }
        } finally {
            genre_cur.close();
        }
    }
    return mGenres;
}

From source file:org.mythtv.service.content.v25.LiveStreamHelperV25.java

private int load(final Context context, final LocationProfile locationProfile, LiveStreamInfo[] liveStreams)
        throws RemoteException, OperationApplicationException {
    Log.d(TAG, "load : enter");

    if (null == context) {
        throw new RuntimeException("LiveStreamHelperV25 is not initialized");
    }/*from  w  w  w.j a  va2s . c o  m*/

    DateTime lastModified = new DateTime(DateTimeZone.UTC);

    int processed = -1;
    int count = 0;

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    for (LiveStreamInfo liveStream : liveStreams) {

        ContentValues values = convertLiveStreamInfoToContentValues(locationProfile, liveStream, lastModified,
                -1, null);

        String[] projection = new String[] { LiveStreamConstants.TABLE_NAME + "_" + LiveStreamConstants._ID };
        String selection = LiveStreamConstants.FIELD_ID + " = ?";
        String[] selectionArgs = new String[] { String.valueOf(liveStream.getId()) };

        selection = appendLocationHostname(context, locationProfile, selection, LiveStreamConstants.TABLE_NAME);

        Cursor cursor = context.getContentResolver().query(LiveStreamConstants.CONTENT_URI, projection,
                selection, selectionArgs, null);
        if (cursor.moveToFirst()) {
            Log.v(TAG, "load : updating existing liveStream info");
            long id = cursor.getLong(cursor
                    .getColumnIndexOrThrow(LiveStreamConstants.TABLE_NAME + "_" + LiveStreamConstants._ID));

            context.getContentResolver().update(ContentUris.withAppendedId(LiveStreamConstants.CONTENT_URI, id),
                    values, null, null);
        }
        cursor.close();
        count++;

        if (count > BATCH_COUNT_LIMIT) {
            Log.i(TAG, "load : applying batch for '" + count + "' transactions, processing programs");

            processBatch(context, ops, processed, count);

            count = 0;

        }

    }

    processBatch(context, ops, processed, count);

    Log.v(TAG, "load : remove deleted liveStreams");
    String deletedSelection = LiveStreamConstants.TABLE_NAME + "." + LiveStreamConstants.FIELD_LAST_MODIFIED
            + " < ?";
    String[] deletedSelectionArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deletedSelection = appendLocationHostname(context, locationProfile, deletedSelection,
            LiveStreamConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(LiveStreamConstants.CONTENT_URI)
            .withSelection(deletedSelection, deletedSelectionArgs).withYieldAllowed(true).build());

    processBatch(context, ops, processed, count);

    Intent progressIntent = new Intent(LiveStreamService.ACTION_PROGRESS);
    context.sendBroadcast(progressIntent);

    if (countLiveStreamsNotComplete(context, locationProfile) > 0) {
        Log.d(TAG, "load : further updates are required");

        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            Log.e(TAG, "load : error", e);
        }

        processed = load(context, locationProfile);
    }

    Log.d(TAG, "load : exit");
    return processed;
}

From source file:org.mythtv.service.content.v26.LiveStreamHelperV26.java

private int load(final Context context, final LocationProfile locationProfile, LiveStreamInfo[] liveStreams)
        throws RemoteException, OperationApplicationException {
    Log.d(TAG, "load : enter");

    if (null == context) {
        throw new RuntimeException("LiveStreamHelperV26 is not initialized");
    }/*from  ww w.  j  av  a2 s . c om*/

    DateTime lastModified = new DateTime(DateTimeZone.UTC);

    int processed = -1;
    int count = 0;

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    for (LiveStreamInfo liveStream : liveStreams) {

        ContentValues values = convertLiveStreamInfoToContentValues(locationProfile, liveStream, lastModified,
                -1, null);

        String[] projection = new String[] { LiveStreamConstants.TABLE_NAME + "_" + LiveStreamConstants._ID };
        String selection = LiveStreamConstants.FIELD_ID + " = ?";
        String[] selectionArgs = new String[] { String.valueOf(liveStream.getId()) };

        selection = appendLocationHostname(context, locationProfile, selection, LiveStreamConstants.TABLE_NAME);

        Cursor cursor = context.getContentResolver().query(LiveStreamConstants.CONTENT_URI, projection,
                selection, selectionArgs, null);
        if (cursor.moveToFirst()) {
            Log.v(TAG, "load : updating existing liveStream info");
            long id = cursor.getLong(cursor
                    .getColumnIndexOrThrow(LiveStreamConstants.TABLE_NAME + "_" + LiveStreamConstants._ID));

            context.getContentResolver().update(ContentUris.withAppendedId(LiveStreamConstants.CONTENT_URI, id),
                    values, null, null);
        }
        cursor.close();
        count++;

        if (count > BATCH_COUNT_LIMIT) {
            Log.i(TAG, "load : applying batch for '" + count + "' transactions, processing programs");

            processBatch(context, ops, processed, count);

            count = 0;

        }

    }

    processBatch(context, ops, processed, count);

    Log.v(TAG, "load : remove deleted liveStreams");
    String deletedSelection = LiveStreamConstants.TABLE_NAME + "." + LiveStreamConstants.FIELD_LAST_MODIFIED
            + " < ?";
    String[] deletedSelectionArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deletedSelection = appendLocationHostname(context, locationProfile, deletedSelection,
            LiveStreamConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(LiveStreamConstants.CONTENT_URI)
            .withSelection(deletedSelection, deletedSelectionArgs).withYieldAllowed(true).build());

    processBatch(context, ops, processed, count);

    Intent progressIntent = new Intent(LiveStreamService.ACTION_PROGRESS);
    context.sendBroadcast(progressIntent);

    if (countLiveStreamsNotComplete(context, locationProfile) > 0) {
        Log.d(TAG, "load : further updates are required");

        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            Log.e(TAG, "load : error", e);
        }

        processed = load(context, locationProfile);
    }

    Log.d(TAG, "load : exit");
    return processed;
}

From source file:org.mythtv.service.content.v27.LiveStreamHelperV27.java

private int load(final Context context, final LocationProfile locationProfile, LiveStreamInfo[] liveStreams)
        throws RemoteException, OperationApplicationException {
    Log.d(TAG, "load : enter");

    if (null == context) {
        throw new RuntimeException("LiveStreamHelperV27 is not initialized");
    }/*  w  w  w .j  a  va  2 s.co  m*/

    DateTime lastModified = new DateTime(DateTimeZone.UTC);

    int processed = -1;
    int count = 0;

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    for (LiveStreamInfo liveStream : liveStreams) {

        ContentValues values = convertLiveStreamInfoToContentValues(locationProfile, liveStream, lastModified,
                -1, null);

        String[] projection = new String[] { LiveStreamConstants.TABLE_NAME + "_" + LiveStreamConstants._ID };
        String selection = LiveStreamConstants.FIELD_ID + " = ?";
        String[] selectionArgs = new String[] { String.valueOf(liveStream.getId()) };

        selection = appendLocationHostname(context, locationProfile, selection, LiveStreamConstants.TABLE_NAME);

        Cursor cursor = context.getContentResolver().query(LiveStreamConstants.CONTENT_URI, projection,
                selection, selectionArgs, null);
        if (cursor.moveToFirst()) {
            Log.v(TAG, "save : updating existing liveStream info");
            long id = cursor.getLong(cursor
                    .getColumnIndexOrThrow(LiveStreamConstants.TABLE_NAME + "_" + LiveStreamConstants._ID));

            context.getContentResolver().update(ContentUris.withAppendedId(LiveStreamConstants.CONTENT_URI, id),
                    values, null, null);
        }
        cursor.close();
        count++;

        if (count > BATCH_COUNT_LIMIT) {
            Log.i(TAG, "load : applying batch for '" + count + "' transactions, processing programs");

            processBatch(context, ops, processed, count);

            count = 0;

        }

    }

    processBatch(context, ops, processed, count);

    Log.v(TAG, "load : remove deleted liveStreams");
    String deletedSelection = LiveStreamConstants.TABLE_NAME + "." + LiveStreamConstants.FIELD_LAST_MODIFIED
            + " < ?";
    String[] deletedSelectionArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deletedSelection = appendLocationHostname(context, locationProfile, deletedSelection,
            LiveStreamConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(LiveStreamConstants.CONTENT_URI)
            .withSelection(deletedSelection, deletedSelectionArgs).withYieldAllowed(true).build());

    processBatch(context, ops, processed, count);

    Intent progressIntent = new Intent(LiveStreamService.ACTION_PROGRESS);
    context.sendBroadcast(progressIntent);

    if (countLiveStreamsNotComplete(context, locationProfile) > 0) {
        Log.d(TAG, "load : further updates are required");

        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            Log.e(TAG, "load : error", e);
        }

        processed = load(context, locationProfile);
    }

    Log.d(TAG, "load : exit");
    return processed;
}

From source file:com.eleybourn.bookcatalogue.BookEdit.java

/**
 * Load a language list; reloading this list every time a tab changes is slow.
 * So we cache it./*  ww  w .  ja v a  2s.  c  o m*/
 * 
 * @return List of languages
 */
@Override
public ArrayList<String> getLanguages() {
    if (mLanguages == null) {
        mLanguages = new ArrayList<String>();
        Cursor cur = mDbHelper.fetchAllLanguages("");
        final int col = cur.getColumnIndexOrThrow(CatalogueDBAdapter.KEY_ROWID);
        try {
            while (cur.moveToNext()) {
                String s = cur.getString(col);
                if (s != null && !s.equals("")) {
                    mLanguages.add(cur.getString(col));
                }
            }
        } finally {
            cur.close();
        }
    }
    return mLanguages;
}

From source file:net.kourlas.voipms_sms.Database.java

/**
 * Gets all of the messages in the database.
 *
 * @return All of the messages in the database.
 *///w w w .ja  v  a 2s. c o m
public synchronized Message[] getMessages() {
    List<Message> messages = new ArrayList<>();

    Cursor cursor = database.query(TABLE_MESSAGE, columns, null, null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Message message = new Message(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATABASE_ID)),
                cursor.isNull(cursor.getColumnIndexOrThrow(COLUMN_VOIP_ID)) ? null
                        : cursor.getLong(cursor.getColumnIndex(COLUMN_VOIP_ID)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TYPE)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DID)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)),
                cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_UNREAD)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELETED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERED)),
                cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERY_IN_PROGRESS)));
        messages.add(message);
        cursor.moveToNext();
    }
    cursor.close();

    Collections.sort(messages);

    Message[] messageArray = new Message[messages.size()];
    return messages.toArray(messageArray);
}

From source file:net.kourlas.voipms_sms.Database.java

/**
 * Gets the database ID for the row in the database with the specified VoIP.ms ID.
 *
 * @param voipId The VoIP.ms ID./* ww  w  . java 2s .  c o m*/
 * @return The database ID.
 */
private synchronized Long getDatabaseIdForVoipId(String did, long voipId) {
    Cursor cursor = database.query(TABLE_MESSAGE, columns,
            COLUMN_DID + "=" + did + " AND " + COLUMN_VOIP_ID + "=" + voipId, null, null, null, null);
    if (cursor.moveToFirst()) {
        return cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATABASE_ID));
    }
    cursor.close();
    return null;
}

From source file:com.eleybourn.bookcatalogue.BookEdit.java

/**
 * Load a publisher list; reloading this list every time a tab changes is
 * slow. So we cache it./*from  w w w  .  java2 s  .  c  o m*/
 * 
 * @return List of publishers
 */
public ArrayList<String> getPublishers() {
    if (mPublishers == null) {
        mPublishers = new ArrayList<String>();
        Cursor publisher_cur = mDbHelper.fetchAllPublishers();
        final int col = publisher_cur.getColumnIndexOrThrow(CatalogueDBAdapter.KEY_PUBLISHER);
        try {
            while (publisher_cur.moveToNext()) {
                mPublishers.add(publisher_cur.getString(col));
            }
        } finally {
            publisher_cur.close();
        }
    }
    return mPublishers;
}