Example usage for android.database Cursor getLong

List of usage examples for android.database Cursor getLong

Introduction

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

Prototype

long getLong(int columnIndex);

Source Link

Document

Returns the value of the requested column as a long.

Usage

From source file:net.olejon.mdapp.MedicationActivity.java

private void getManufacturer() {
    SQLiteDatabase sqLiteDatabase = new SlDataSQLiteHelper(mContext).getReadableDatabase();

    String[] queryColumns = { SlDataSQLiteHelper.MANUFACTURERS_COLUMN_ID };
    Cursor cursor = sqLiteDatabase.query(SlDataSQLiteHelper.TABLE_MANUFACTURERS, queryColumns,
            SlDataSQLiteHelper.MANUFACTURERS_COLUMN_NAME + " = " + mTools.sqe(medicationManufacturer), null,
            null, null, null);//from w  w w . j a  v  a  2  s. c  o  m

    if (cursor.moveToFirst()) {
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(SlDataSQLiteHelper.MANUFACTURERS_COLUMN_ID));

        Intent intent = new Intent(mContext, ManufacturerActivity.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }

    cursor.close();
    sqLiteDatabase.close();
}

From source file:gov.wa.wsdot.android.wsdot.service.FerriesTerminalSailingSpaceSyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = null;
    long now = System.currentTimeMillis();
    boolean shouldUpdate = true;
    String responseString = "";
    DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy h:mm a");

    /** //w  w w. ja  v a 2s .co m
     * Check the cache table for the last time data was downloaded. If we are within
     * the allowed time period, don't sync, otherwise get fresh data from the server.
     */
    try {
        cursor = resolver.query(Caches.CONTENT_URI, new String[] { Caches.CACHE_LAST_UPDATED },
                Caches.CACHE_TABLE_NAME + " LIKE ?", new String[] { "ferries_terminal_sailing_space" }, null);

        if (cursor != null && cursor.moveToFirst()) {
            long lastUpdated = cursor.getLong(0);
            shouldUpdate = (Math.abs(now - lastUpdated) > (15 * DateUtils.SECOND_IN_MILLIS));
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    // Ability to force a refresh of camera data.
    boolean forceUpdate = intent.getBooleanExtra("forceUpdate", false);

    if (shouldUpdate || forceUpdate) {
        List<Integer> starred = new ArrayList<Integer>();

        starred = getStarred();

        try {
            URL url = new URL(TERMINAL_SAILING_SPACE_URL);
            URLConnection urlConn = url.openConnection();

            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            String jsonFile = "";
            String line;

            while ((line = in.readLine()) != null)
                jsonFile += line;
            in.close();

            JSONArray array = new JSONArray(jsonFile);
            List<ContentValues> terminal = new ArrayList<ContentValues>();

            int numItems = array.length();
            for (int j = 0; j < numItems; j++) {
                JSONObject item = array.getJSONObject(j);
                ContentValues sailingSpaceValues = new ContentValues();
                sailingSpaceValues.put(FerriesTerminalSailingSpace.TERMINAL_ID, item.getInt("TerminalID"));
                sailingSpaceValues.put(FerriesTerminalSailingSpace.TERMINAL_NAME,
                        item.getString("TerminalName"));
                sailingSpaceValues.put(FerriesTerminalSailingSpace.TERMINAL_ABBREV,
                        item.getString("TerminalAbbrev"));
                sailingSpaceValues.put(FerriesTerminalSailingSpace.TERMINAL_DEPARTING_SPACES,
                        item.getString("DepartingSpaces"));
                sailingSpaceValues.put(FerriesTerminalSailingSpace.TERMINAL_LAST_UPDATED,
                        dateFormat.format(new Date(System.currentTimeMillis())));

                if (starred.contains(item.getInt("TerminalID"))) {
                    sailingSpaceValues.put(FerriesTerminalSailingSpace.TERMINAL_IS_STARRED, 1);
                }

                terminal.add(sailingSpaceValues);
            }

            // Purge existing terminal sailing space items covered by incoming data
            resolver.delete(FerriesTerminalSailingSpace.CONTENT_URI, null, null);
            // Bulk insert all the new terminal sailing space items
            resolver.bulkInsert(FerriesTerminalSailingSpace.CONTENT_URI,
                    terminal.toArray(new ContentValues[terminal.size()]));
            // Update the cache table with the time we did the update
            ContentValues values = new ContentValues();
            values.put(Caches.CACHE_LAST_UPDATED, System.currentTimeMillis());
            resolver.update(Caches.CONTENT_URI, values, Caches.CACHE_TABLE_NAME + "=?",
                    new String[] { "ferries_terminal_sailing_space" });

            responseString = "OK";

        } catch (Exception e) {
            Log.e(TAG, "Error: " + e.getMessage());
            responseString = e.getMessage();
        }
    } else {
        responseString = "NOP";
    }

    Intent broadcastIntent = new Intent();
    broadcastIntent
            .setAction("gov.wa.wsdot.android.wsdot.intent.action.FERRIES_TERMINAL_SAILING_SPACE_RESPONSE");
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("responseString", responseString);
    sendBroadcast(broadcastIntent);
}

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

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    final String appId = getCallingActivityId();
    if (appId == null) {
        Log.d(TAG, "No AppId for calling activity. Ignoring query.");
        return 0;
    }//from w w w .  j ava  2  s .co m
    String appSelection = DbObject.APP_ID + "= ?";
    String[] appSelectionArgs = new String[] { appId };
    selection = DBHelper.andClauses(selection, appSelection);
    selectionArgs = DBHelper.andArguments(selectionArgs, appSelectionArgs);
    String[] projection = new String[] { DbObject.HASH };

    int count = 0;
    Cursor c = mHelper.getReadableDatabase().query(DbObject.TABLE, projection, selection, selectionArgs, null,
            null, null);
    if (c != null && c.moveToFirst()) {
        count = c.getCount();
        long[] hashes = new long[count];
        int i = 0;
        do {
            hashes[i++] = c.getLong(0);
        } while (c.moveToNext());
        Helpers.sendToFeed(getContext(), DeleteObj.from(hashes, true), uri);
    }
    return count;
}

From source file:com.zegoggles.smssync.CursorToMessage.java

public PersonRecord lookupPerson(final String address) {
    if (!mPeopleCache.containsKey(address)) {
        Uri personUri = Uri.withAppendedPath(
                NEW_CONTACT_API ? ECLAIR_CONTENT_FILTER_URI : Phones.CONTENT_FILTER_URL, Uri.encode(address));

        Cursor c = mContext.getContentResolver().query(personUri, PHONE_PROJECTION, null, null, null);
        final PersonRecord record = new PersonRecord();
        if (c != null && c.moveToFirst()) {
            record._id = c.getLong(c.getColumnIndex(PHONE_PROJECTION[0]));
            record.name = sanitize(c.getString(c.getColumnIndex(PHONE_PROJECTION[1])));
            record.number = sanitize(/*from ww w .  ja  v  a  2 s.  c  om*/
                    NEW_CONTACT_API ? address : c.getString(c.getColumnIndex(PHONE_PROJECTION[2])));
            record.email = getPrimaryEmail(record._id, record.number);
        } else {
            if (LOCAL_LOGV)
                Log.v(TAG, "Looked up unknown address: " + address);

            record.number = sanitize(address);
            record.email = getUnknownEmail(address);
            record.unknown = true;
        }
        mPeopleCache.put(address, record);

        if (c != null)
            c.close();
    }
    return mPeopleCache.get(address);
}

From source file:com.android.contacts.common.model.ContactLoader.java

/**
 * Extracts RawContact level columns from the cursor.
 *//* w  w w  .j a v  a  2s  .  co  m*/
private ContentValues loadRawContactValues(Cursor cursor) {
    ContentValues cv = new ContentValues();

    cv.put(RawContacts._ID, cursor.getLong(ContactQuery.RAW_CONTACT_ID));

    cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_NAME);
    cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SET);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DIRTY);
    cursorColumnToContentValues(cursor, cv, ContactQuery.VERSION);
    cursorColumnToContentValues(cursor, cv, ContactQuery.SOURCE_ID);
    cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC1);
    cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC2);
    cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC3);
    cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC4);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DELETED);
    cursorColumnToContentValues(cursor, cv, ContactQuery.CONTACT_ID);
    cursorColumnToContentValues(cursor, cv, ContactQuery.STARRED);

    return cv;
}

From source file:com.ichi2.anki.tests.ContentProviderTest.java

/**
 * Move all the cards from their old decks to the first deck that was added in setup()
 *///  w  w w  . ja  v  a2s .  co  m
public void testMoveCardsToOtherDeck() {
    final ContentResolver cr = getContext().getContentResolver();
    // Query all available notes
    final Cursor allNotesCursor = cr.query(FlashCardsContract.Note.CONTENT_URI, null, "tag:" + TEST_TAG, null,
            null);
    assertNotNull(allNotesCursor);
    try {
        assertEquals("Check number of results", mCreatedNotes.size(), allNotesCursor.getCount());
        while (allNotesCursor.moveToNext()) {
            // Now iterate over all cursors
            Uri cardsUri = Uri.withAppendedPath(
                    Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI,
                            allNotesCursor
                                    .getString(allNotesCursor.getColumnIndex(FlashCardsContract.Note._ID))),
                    "cards");
            final Cursor cardsCursor = cr.query(cardsUri, null, null, null, null);
            assertNotNull("Check that there is a valid cursor after query for cards", cardsCursor);
            try {
                assertTrue("Check that there is at least one result for cards", cardsCursor.getCount() > 0);
                while (cardsCursor.moveToNext()) {
                    long targetDid = mTestDeckIds[0];
                    // Move to test deck
                    ContentValues values = new ContentValues();
                    values.put(FlashCardsContract.Card.DECK_ID, targetDid);
                    Uri cardUri = Uri.withAppendedPath(cardsUri, cardsCursor
                            .getString(cardsCursor.getColumnIndex(FlashCardsContract.Card.CARD_ORD)));
                    cr.update(cardUri, values, null, null);
                    Cursor movedCardCur = cr.query(cardUri, null, null, null, null);
                    assertNotNull("Check that there is a valid cursor after moving card", movedCardCur);
                    assertTrue("Move to beginning of cursor after moving card", movedCardCur.moveToFirst());
                    long did = movedCardCur
                            .getLong(movedCardCur.getColumnIndex(FlashCardsContract.Card.DECK_ID));
                    assertEquals("Make sure that card is in new deck", targetDid, did);
                }
            } finally {
                cardsCursor.close();
            }
        }
    } finally {
        allNotesCursor.close();
    }
}

From source file:io.n7.calendar.caldav.CalDAVService.java

private VEvent getEvFromDB(String uid, long id) {
    String[] proj = { Events.TITLE, //0
            Events.DESCRIPTION, //1
            Events.DTSTART, //2 
            Events.DTEND, //3
            Events.EVENT_TIMEZONE, //4
            Events.DURATION, //5
            Events.EVENT_LOCATION }; //6

    Cursor c = mCR.query(ContentUris.withAppendedId(EVENTS_URI, id), proj, null, null, null);
    if (c.moveToFirst()) {
        VEvent ve = new VEvent();
        ve.getProperties().add(new Summary(c.getString(0)));
        ve.getProperties().add(new Description(c.getString(1)));
        ve.getProperties().add(new DtStart(new DateTime(c.getLong(2))));
        ve.getProperties().add(new TzId(c.getString(4)));
        ve.getProperties().add(new Location(c.getString(6)));

        long tl = c.getLong(3);
        if (tl > 0) {
            ve.getProperties().add(new DtEnd(new DateTime(tl)));
            Log.d(TAG, "dt end " + tl);
        } else {/*from  w  w  w .ja v  a2  s . co m*/
            String ts = c.getString(4);
            Log.d(TAG, "dur " + ts);
            if (ts != null) {
                net.fortuna.ical4j.model.property.Duration dur = new net.fortuna.ical4j.model.property.Duration();
                dur.setValue(ts);
                ve.getProperties().add(dur);
            }
        }
        ve.getProperties().add(new Uid(uid));
        return ve;
    } else
        return null;
}

From source file:com.android.contacts.common.model.ContactLoader.java

/**
 * Extracts Data level columns from the cursor.
 *///ww  w  .j a v  a2  s  .  co m
private ContentValues loadDataValues(Cursor cursor) {
    ContentValues cv = new ContentValues();

    cv.put(Data._ID, cursor.getLong(ContactQuery.DATA_ID));

    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA1);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA2);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA3);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA4);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA5);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA6);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA7);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA8);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA9);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA10);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA11);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA12);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA13);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA14);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA15);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC1);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC2);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC3);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC4);
    cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_VERSION);
    cursorColumnToContentValues(cursor, cv, ContactQuery.IS_PRIMARY);
    cursorColumnToContentValues(cursor, cv, ContactQuery.IS_SUPERPRIMARY);
    cursorColumnToContentValues(cursor, cv, ContactQuery.MIMETYPE);
    cursorColumnToContentValues(cursor, cv, ContactQuery.GROUP_SOURCE_ID);
    cursorColumnToContentValues(cursor, cv, ContactQuery.CHAT_CAPABILITY);
    cursorColumnToContentValues(cursor, cv, ContactQuery.TIMES_USED);
    cursorColumnToContentValues(cursor, cv, ContactQuery.LAST_TIME_USED);

    return cv;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static U1Node getNodeByKey(String key) {
    String[] selectionArgs = new String[] { key };
    String selection = Nodes.NODE_KEY + "=?";
    String[] projection = Nodes.getDefaultProjection();
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {/*w  w  w .j  av a  2s .c  o m*/
            if (c.moveToFirst()) {
                String resourcePath;
                U1NodeKind kind;
                Boolean isLive = true;
                String path;
                String parentPath;
                String volumePath;
                Date whenCreated;
                Date whenChanged;
                Long generation;
                Long generationCreated;
                String contentPath;

                resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                kind = U1NodeKind
                        .valueOf(c.getString(c.getColumnIndex(Nodes.NODE_KIND)).toUpperCase(Locale.US));
                isLive = c.getInt(c.getColumnIndex(Nodes.NODE_IS_LIVE)) != 0;
                path = c.getString(c.getColumnIndex(Nodes.NODE_PATH));
                parentPath = c.getString(c.getColumnIndex(Nodes.NODE_PARENT_PATH));
                volumePath = c.getString(c.getColumnIndex(Nodes.NODE_VOLUME_PATH));
                whenCreated = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CREATED)));
                whenChanged = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CHANGED)));
                generation = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION));
                generationCreated = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION_CREATED));
                contentPath = c.getString(c.getColumnIndex(Nodes.NODE_CONTENT_PATH));

                return new U1Node(resourcePath, kind, isLive, path, parentPath, volumePath, key, whenCreated,
                        whenChanged, generation, generationCreated, contentPath);
            } else {
                return null;
            }
        } finally {
            c.close();
        }
    }
    return null;
}

From source file:com.android.browser.BookmarksPageCallbacks.java

private void populateBookmarkItem(Cursor cursor, BookmarkItem item, boolean isFolder) {
    item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
    if (isFolder) {
        item.setUrl(null);// ww  w.j  a  va  2  s  . co m
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_folder_holo_dark);
        item.setFavicon(bitmap);
        new LookupBookmarkCount(getActivity(), item).execute(cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID));
    } else {
        String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
        item.setUrl(url);
        Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
        item.setFavicon(bitmap);
    }
}