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:com.chatwingsdk.fragments.CommunicationDrawerFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    TextView unreadCountView;// www  .  ja  v a  2s  .co m
    switch (loader.getId()) {
    case LOADER_ID_COUNT_CONVERSATIONS_MESSAGES_UNREAD:
        unreadCountView = mConversationsUnreadCountView;
        break;
    case LOADER_ID_CHATBOXES:
        unreadCountView = mCategoriesUnreadCountView;
        break;
    default:
        return;
    }

    if (data == null || data.getCount() == 0 || !data.moveToFirst()) {
        unreadCountView.setVisibility(View.GONE);
        return;
    }

    int columnIndex = data.getColumnIndexOrThrow(COLUMN_NAME_SUM_UNREAD_COUNT);
    int unreadCount = data.getInt(columnIndex);
    if (unreadCount == 0) {
        unreadCountView.setVisibility(View.GONE);
    } else {
        unreadCountView.setText(Integer.toString(unreadCount));
        unreadCountView.setVisibility(View.VISIBLE);
    }
}

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

private List<Long> getFeedSubscribers(Uri feedUri) {
    if (feedUri == null) {
        throw new NullPointerException("Feed cannot be null");
    }/*  www  . j  av a 2s .c  o m*/
    String feedName = feedUri.getLastPathSegment();
    switch (Feed.typeOf(feedUri)) {
    case FRIEND:
        String personId = Feed.friendIdForFeed(feedUri);
        if (personId == null) {
            return new ArrayList<Long>(0);
        }
        String table = Contact.TABLE;
        String[] columns = new String[] { Contact._ID };
        String selection = Contact.PERSON_ID + " = ?";
        String[] selectionArgs = new String[] { personId };
        String groupBy = null;
        String having = null;
        String orderBy = null;
        Cursor c = mHelper.getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy,
                having, orderBy);
        if (c == null || !c.moveToFirst()) {
            Log.w(TAG, "Could not find user for id " + personId);
            return new ArrayList<Long>(0);
        }
        return Collections.singletonList(c.getLong(0));
    case APP:
        // Currently, we send app messages to all users, which are registered
        // as subscribers to the "friend" feed. The subscribers model needs to
        // be reworked, and further the "app" feed needs further thinking.
        // Messages should be lossy, and encryption should not require keys
        // for each recipient.
        feedName = "friend";
        // No break:
    case GROUP:
        Cursor subs = mHelper.querySubscribers(feedName);
        List<Long> recipientIds = new ArrayList<Long>(subs.getCount());
        subs.moveToFirst();
        while (!subs.isAfterLast()) {
            long id = subs.getLong(subs.getColumnIndexOrThrow(Subscriber.CONTACT_ID));
            recipientIds.add(id);
            subs.moveToNext();
        }
        subs.close();
        return recipientIds;
    default:
        Log.w(TAG, "unmatched feed type for " + feedUri);
        return new ArrayList<Long>();
    }
}

From source file:org.mythtv.service.dvr.v27.RecordingRuleHelperV27.java

private void processRecordingRule(final Context context, final LocationProfile locationProfile,
        ArrayList<ContentProviderOperation> ops, org.mythtv.services.api.v027.beans.RecRule recRule) {
    Log.d(TAG, "processRecordingRule : enter");

    String recRuleSelection = RecordingRuleConstants.FIELD_REC_RULE_ID + " = ?";

    recRuleSelection = appendLocationHostname(context, locationProfile, recRuleSelection,
            RecordingRuleConstants.TABLE_NAME);

    ContentValues recRuleValues = convertRecRuleToContentValues(locationProfile, recRule);
    Cursor recRuleCursor = context.getContentResolver().query(RecordingRuleConstants.CONTENT_URI,
            recRuleProjection, recRuleSelection, new String[] { String.valueOf(recRule.getId()) }, null);
    if (recRuleCursor.moveToFirst()) {
        Long id = recRuleCursor.getLong(recRuleCursor.getColumnIndexOrThrow(RecordingRuleConstants._ID));
        Log.v(TAG, "processRecordingRule : updating recRule " + id + ":" + recRule.getId() + ":"
                + recRule.getTitle());//ww w .ja va  2 s  . c  o  m

        ops.add(ContentProviderOperation
                .newUpdate(ContentUris.withAppendedId(RecordingRuleConstants.CONTENT_URI, id))
                .withValues(recRuleValues).build());

    } else {
        Log.v(TAG, "processRecordingRule : adding recRule " + recRule.getId() + ":" + recRule.getTitle());

        ops.add(ContentProviderOperation.newInsert(RecordingRuleConstants.CONTENT_URI).withValues(recRuleValues)
                .build());

    }
    recRuleCursor.close();

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

From source file:org.mythtv.service.dvr.v26.RecordedHelperV26.java

private void processProgramGroups(final Context context, final LocationProfile locationProfile,
        Program[] programs) throws RemoteException, OperationApplicationException {
    Log.v(TAG, "processProgramGroups : enter");

    if (null == context)
        throw new RuntimeException("RecordedHelperV26 is not initialized");

    Map<String, ProgramGroup> programGroups = new TreeMap<String, ProgramGroup>();
    for (Program program : programs) {

        if (null != program.getRecording()) {

            if (null != program.getRecording().getRecGroup()
                    && !"livetv".equalsIgnoreCase(program.getRecording().getRecGroup())
                    && !"deleted".equalsIgnoreCase(program.getRecording().getRecGroup())) {
                String cleaned = ArticleCleaner.clean(program.getTitle());
                if (!programGroups.containsKey(cleaned)) {

                    ProgramGroup programGroup = new ProgramGroup();
                    programGroup.setTitle(program.getTitle());
                    programGroup.setCategory(program.getCategory());
                    programGroup.setInetref(program.getInetref());
                    programGroup.setSort(0);

                    programGroups.put(cleaned, programGroup);
                }//  ww w  .j  a  v  a 2 s .  com

            }

        }

    }

    int processed = -1;
    int count = 0;

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

    Log.v(TAG, "processProgramGroups : adding 'All' program group in programGroups");
    ProgramGroup all = new ProgramGroup(null, "All", "All", "All", "", 1);
    programGroups.put(all.getProgramGroup(), all);

    String[] programGroupProjection = new String[] { ProgramGroupConstants._ID };
    String programGroupSelection = ProgramGroupConstants.FIELD_PROGRAM_GROUP + " = ?";

    programGroupSelection = appendLocationHostname(context, locationProfile, programGroupSelection, null);

    for (String key : programGroups.keySet()) {
        Log.v(TAG, "processProgramGroups : processing programGroup '" + key + "'");

        ProgramGroup programGroup = programGroups.get(key);

        ContentValues programValues = convertProgramGroupToContentValues(locationProfile, programGroup);
        Cursor programGroupCursor = context.getContentResolver().query(ProgramGroupConstants.CONTENT_URI,
                programGroupProjection, programGroupSelection, new String[] { key }, null);
        if (programGroupCursor.moveToFirst()) {

            Long id = programGroupCursor
                    .getLong(programGroupCursor.getColumnIndexOrThrow(ProgramGroupConstants._ID));
            ops.add(ContentProviderOperation
                    .newUpdate(ContentUris.withAppendedId(ProgramGroupConstants.CONTENT_URI, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {

            ops.add(ContentProviderOperation.newInsert(ProgramGroupConstants.CONTENT_URI)
                    .withValues(programValues).withYieldAllowed(true).build());
        }
        programGroupCursor.close();
        count++;

        if (count > 100) {
            Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

            processBatch(context, ops, processed, count);
        }

    }

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : remove deleted program groups");
    ops = new ArrayList<ContentProviderOperation>();

    DateTime lastModified = new DateTime();
    lastModified = lastModified.minusHours(1);

    String deleteProgramGroupSelection = ProgramGroupConstants.FIELD_LAST_MODIFIED_DATE + " < ?";
    String[] deleteProgramGroupArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deleteProgramGroupSelection = appendLocationHostname(context, locationProfile, deleteProgramGroupSelection,
            ProgramGroupConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(ProgramGroupConstants.CONTENT_URI)
            .withSelection(deleteProgramGroupSelection, deleteProgramGroupArgs).withYieldAllowed(true).build());

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : exit");
}

From source file:com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.java

/**
 * Pauses all transfers which have the given type.
 *
 * @param type The type of transfers/*from   ww w.j  a  v a 2s .c  om*/
 */
public void pauseAllWithType(TransferType type) {
    Cursor c = null;
    try {
        c = dbUtil.queryAllTransfersWithType(type);
        while (c.moveToNext()) {
            final int id = c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_ID));
            pause(id);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.java

/**
 * Sets all transfers which have the given type to be canceled. Note the
 * TransferState must be TransferState.CANCELED before the transfer is
 * guaranteed to have stopped, and can be safely deleted
 *
 * @param type The type of transfers/*w w  w. ja  va2s  .co  m*/
 */
public void cancelAllWithType(TransferType type) {
    Cursor c = null;
    try {
        c = dbUtil.queryAllTransfersWithType(type);
        while (c.moveToNext()) {
            final int id = c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_ID));
            cancel(id);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:org.mythtv.service.dvr.v25.RecordedHelperV25.java

private void processProgramGroups(final Context context, final LocationProfile locationProfile,
        Program[] programs) throws RemoteException, OperationApplicationException {
    Log.v(TAG, "processProgramGroups : enter");

    if (null == context)
        throw new RuntimeException("RecordedHelperV25 is not initialized");

    Map<String, ProgramGroup> programGroups = new TreeMap<String, ProgramGroup>();
    for (Program program : programs) {

        if (null != program.getRecording()) {

            if (null != program.getRecording().getRecGroup()
                    && !"livetv".equalsIgnoreCase(program.getRecording().getRecGroup())
                    && !"deleted".equalsIgnoreCase(program.getRecording().getRecGroup())) {
                String cleaned = ArticleCleaner.clean(program.getTitle());
                if (!programGroups.containsKey(cleaned)) {

                    ProgramGroup programGroup = new ProgramGroup();
                    programGroup.setTitle(program.getTitle());
                    programGroup.setCategory(program.getCategory());
                    programGroup.setInetref(program.getInetref());
                    programGroup.setSort(0);

                    programGroups.put(cleaned, programGroup);
                }/*www .java  2 s .c  o m*/

            }

        }

    }

    int processed = -1;
    int count = 0;

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

    Log.v(TAG, "processProgramGroups : adding 'All' program group in programGroups");
    ProgramGroup all = new ProgramGroup(null, "All", "All", "All", "", 1);
    programGroups.put(all.getProgramGroup(), all);

    String[] programGroupProjection = new String[] { ProgramGroupConstants._ID };
    String programGroupSelection = ProgramGroupConstants.FIELD_PROGRAM_GROUP + " = ?";

    programGroupSelection = appendLocationHostname(context, locationProfile, programGroupSelection, null);

    for (String key : programGroups.keySet()) {
        Log.v(TAG, "processProgramGroups : processing programGroup '" + key + "'");

        ProgramGroup programGroup = programGroups.get(key);

        ContentValues programValues = convertProgramGroupToContentValues(locationProfile, programGroup);
        Cursor programGroupCursor = context.getContentResolver().query(ProgramGroupConstants.CONTENT_URI,
                programGroupProjection, programGroupSelection, new String[] { key }, null);
        if (programGroupCursor.moveToFirst()) {

            Long id = programGroupCursor
                    .getLong(programGroupCursor.getColumnIndexOrThrow(ProgramGroupConstants._ID));
            ops.add(ContentProviderOperation
                    .newUpdate(ContentUris.withAppendedId(ProgramGroupConstants.CONTENT_URI, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {

            ops.add(ContentProviderOperation.newInsert(ProgramGroupConstants.CONTENT_URI)
                    .withValues(programValues).withYieldAllowed(true).build());
        }
        programGroupCursor.close();
        count++;

        if (count > 100) {
            Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

            processBatch(context, ops, processed, count);
        }

    }

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : remove deleted program groups");
    ops = new ArrayList<ContentProviderOperation>();

    DateTime lastModified = new DateTime();
    lastModified = lastModified.minusHours(1);

    String deleteProgramGroupSelection = ProgramGroupConstants.FIELD_LAST_MODIFIED_DATE + " < ?";
    String[] deleteProgramGroupArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deleteProgramGroupSelection = appendLocationHostname(context, locationProfile, deleteProgramGroupSelection,
            ProgramGroupConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(ProgramGroupConstants.CONTENT_URI)
            .withSelection(deleteProgramGroupSelection, deleteProgramGroupArgs).withYieldAllowed(true).build());

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : exit");
}

From source file:com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.java

/**
 * Gets a list of TransferObserver instances which are observing records
 * with the given type.//from ww  w .  jav  a  2 s  .c  o m
 *
 * @param type The type of the transfer "any".
 * @return A list of TransferObserver instances.
 */
public List<TransferObserver> getTransfersWithType(TransferType type) {
    final List<TransferObserver> transferObservers = new ArrayList<TransferObserver>();
    Cursor c = null;
    try {
        c = dbUtil.queryAllTransfersWithType(type);
        while (c.moveToNext()) {
            final int id = c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_ID));
            final TransferObserver to = new TransferObserver(id, dbUtil);
            to.updateFromDB(c);
            transferObservers.add(to);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return transferObservers;
}

From source file:com.dvn.vindecoder.ui.seller.AddVehicalAndPayment.java

private String getRealPathFromUri(Uri tempUri) {
    Cursor cursor = null;
    try {/*from   w  ww. jav  a  2s.  co m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = this.getContentResolver().query(tempUri, 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.ezac.gliderlogs.FlightReportActivity.java

private void fillData(Uri uri) {

    String selection;// w  w  w  . j a v a 2s  .c om
    Cursor cursor;
    /*
     * alternative to aggregated query, it doesn't seem to be not
     * supported by getContentResolver().query
     */
    GliderList.clear();
    GliderListCount.clear();
    GliderListTime.clear();
    stringArrayList.clear();
    String[] projection = { GliderLogTables.F_ID, GliderLogTables.F_REGISTRATION, GliderLogTables.F_DURATION };
    selection = "(" + GliderLogTables.F_STARTED + " IS NOT '' AND " + GliderLogTables.F_LANDED + " IS NOT '')";
    cursor = getContentResolver().query(uri, projection, selection, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();
        do {
            String res = cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_REGISTRATION))
                    .toUpperCase(Locale.US);
            if (GliderList.contains(res)) {
                int j = GliderList.indexOf(res);
                GliderListCount.set(j, GliderListCount.get(j) + 1);
                GliderListTime.set(j, GliderListTime.get(j)
                        + ToMinute(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_DURATION))));
            } else {
                GliderList.add(res);
                GliderListCount.add(1);
                GliderListTime.add(
                        ToMinute(cursor.getString(cursor.getColumnIndexOrThrow(GliderLogTables.F_DURATION))));
            }
        } while (cursor.moveToNext());
        int GrandCnt = 0;
        int GrandTime = 0;
        for (int i = 0; i < GliderList.size(); i++) {
            stringArrayList.add(GliderList.get(i));
            stringArrayList.add(GliderListCount.get(i).toString());
            GrandCnt = GrandCnt + GliderListCount.get(i);
            int hour = GliderListTime.get(i) / 60;
            int minute = GliderListTime.get(i) - (hour * 60);
            stringArrayList.add("" + hour + ":" + Common.TwoDigits(minute));
            GrandTime = GrandTime + GliderListTime.get(i);
        }
        // added as to generate a grand total
        stringArrayList.add("Totaal");
        stringArrayList.add("" + GrandCnt);
        int hour = GrandTime / 60;
        int minute = GrandTime - (hour * 60);
        stringArrayList.add("" + hour + ":" + Common.TwoDigits(minute));
    }
    cursor.close();
}