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:com.iyedb.sunshine.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName        A human-readable city name, e.g "Mountain View"
 * @param lat             the latitude of the city
 * @param lon             the longitude of the city
 * @return the row ID of the added location.
 *///from  w w  w. j a  v  a2s  .c o  m
private long insertLocationInDatabase(String locationSetting, String cityName, double lat, double lon) {

    Log.v(LOG_TAG, "inserting " + cityName + ", with coord: " + lat + ", " + lon);

    // First, check if the location with this city name exists in the db
    Cursor cursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI,
            new String[] { LocationEntry._ID }, LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] { locationSetting }, null);

    if (cursor.moveToFirst()) {
        Log.v(LOG_TAG, "Found it in the database!");
        int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        ContentValues locationValues = new ContentValues();
        locationValues.put(LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(LocationEntry.COLUMN_COORD_LONG, lon);

        Uri locationInsertUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, locationValues);

        return ContentUris.parseId(locationInsertUri);
    }
}

From source file:edu.msu.walajahi.sunshine.app.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city/*from  w ww  . j ava2  s.  com*/
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    // Students: First, check if the location with this city name exists in the db
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI

    long locationId;

    // First, check if the location with this city name exists in the db
    Cursor locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (locationCursor.moveToFirst()) {
        int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } else {
        // Now that the content provider is set up, inserting rows of data is pretty simple.
        // First create a ContentValues object to hold the data you want to insert.
        ContentValues locationValues = new ContentValues();

        // Then add the data, along with the corresponding name of the data type,
        // so the content provider knows what kind of value is being inserted.
        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        // Finally, insert location data into the database.
        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);

        // The resulting URI contains the ID for the row.  Extract the locationId from the Uri.
        locationId = ContentUris.parseId(insertedUri);
    }

    locationCursor.close();
    // Wait, that worked?  Yes!
    return locationId;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;//from   ww  w  . ja  va2 s  .c  om
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        // String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        // return new HistoryItem(result, display, details);
        return new HistoryItem(result, display);
    } finally {
        close(cursor, db);
    }
}

From source file:com.navjagpal.fileshare.WebServer.java

private void sendPlaylist(DefaultHttpServerConnection serverConnection, RequestLine requestLine)
        throws IOException, HttpException {
    HttpResponse response = new BasicHttpResponse(new HttpVersion(1, 1), 200, "OK");

    Cursor c = mContext.getContentResolver().query(FileSharingProvider.Files.CONTENT_URI, new String[] {
            FileSharingProvider.Files.Columns._ID, FileSharingProvider.Files.Columns.DISPLAY_NAME }, null, null,
            null);/*w  w  w .ja v  a 2  s  .  c  o  m*/
    String playlist = "";
    while (c.moveToNext()) {
        long id = c.getLong(c.getColumnIndex(FileSharingProvider.Files.Columns._ID));
        String name = c.getString(c.getColumnIndex(FileSharingProvider.Files.Columns.DISPLAY_NAME));
        if (name.endsWith(".mp3")) {
            playlist += SharedFileBrowser.getShareURL(id, mContext) + "\n";
        }
    }
    c.close();
    response.addHeader("Content-Type", "audio/x-mpegurl");
    response.addHeader("Content-Length", "" + playlist.length());
    response.setEntity(new StringEntity(playlist));
    serverConnection.sendResponseHeader(response);
    serverConnection.sendResponseEntity(response);
}

From source file:com.yuntongxun.ecdemo.storage.IMessageSqlManager.java

/**
 * ???ID//from  w ww  .j  av  a  2 s .c om
 *
 * @param contactId
 * @return
 */
public static long querySessionIdForByContactId(String contactId) {
    Cursor cursor = null;
    long threadId = 0;
    if (contactId != null) {
        String where = IThreadColumn.CONTACT_ID + " = '" + contactId + "' ";
        try {
            cursor = getInstance().sqliteDB().query(DatabaseHelper.TABLES_NAME_IM_SESSION, null, where, null,
                    null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                if (cursor.moveToFirst()) {
                    threadId = cursor.getLong(cursor.getColumnIndexOrThrow(IThreadColumn.ID));
                }
            }
        } catch (SQLException e) {
            LogUtil.e(TAG + " " + e.toString());
        } finally {
            if (cursor != null) {
                cursor.close();
                cursor = null;
            }
        }
    }
    return threadId;
}

From source file:com.example.asaldanha.sunshine.app.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city//w  w w .  j  a v  a  2 s .c o  m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
public long addLocation(String locationSetting, String cityName, double lat, double lon) {

    long locationID;

    // Students: First, check if the location with this city name exists in the db
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI

    Cursor locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (locationCursor.moveToFirst()) {
        int locationIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationID = locationCursor.getLong(locationIndex);
    } else {

        //Create a new set of content values
        ContentValues locationValues = new ContentValues();

        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);

        Uri insertURI = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);
        locationID = ContentUris.parseId(insertURI);
    }
    return locationID;
}

From source file:com.example.android.miniweather.app.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city//  w w  w  . j  av  a  2  s  . co  m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    // Students: First, check if the location with this city name exists in the db
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI

    ContentResolver contentResolver = mContext.getContentResolver();
    Cursor cursor;
    long locationID;

    cursor = contentResolver.query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (cursor.moveToFirst()) {
        int locationIDIndex = cursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationID = cursor.getLong(locationIDIndex);
    } else {
        ContentValues newLocation = new ContentValues();
        Uri insertedUri;

        newLocation.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        newLocation.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        newLocation.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        newLocation.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        insertedUri = contentResolver.insert(WeatherContract.LocationEntry.CONTENT_URI, newLocation);

        locationID = ContentUris.parseId(insertedUri);

    }

    cursor.close();
    return locationID;
}

From source file:com.google.android.apps.iosched.ui.SessionDetailActivity.java

/** Handle {@link SessionsQuery} {@link Cursor}. */
private void onSessionQueryComplete(Cursor cursor) {
    try {// w  ww.  jav  a 2s .com
        mSessionCursor = true;
        if (!cursor.moveToFirst())
            return;

        // Format time block this session occupies
        final long blockStart = cursor.getLong(SessionsQuery.BLOCK_START);
        final long blockEnd = cursor.getLong(SessionsQuery.BLOCK_END);
        final String roomName = cursor.getString(SessionsQuery.ROOM_NAME);
        final String subtitle = UIUtils.formatSessionSubtitle(blockStart, blockEnd, roomName, this);

        mTitleString = cursor.getString(SessionsQuery.TITLE);
        mTitle.setText(mTitleString);
        mSubtitle.setText(subtitle);

        mHashtag = cursor.getString(SessionsQuery.HASHTAG);
        if (TextUtils.isEmpty(mHashtag))
            mHashtag = "";

        mRoomId = cursor.getString(SessionsQuery.ROOM_ID);

        // Unregister around setting checked state to avoid triggering
        // listener since change isn't user generated.
        mStarred.setOnCheckedChangeListener(null);
        mStarred.setChecked(cursor.getInt(SessionsQuery.STARRED) != 0);
        mStarred.setOnCheckedChangeListener(this);

        final String sessionAbstract = cursor.getString(SessionsQuery.ABSTRACT);
        if (!TextUtils.isEmpty(sessionAbstract)) {
            UIUtils.setTextMaybeHtml(mAbstract, sessionAbstract);
            mAbstract.setVisibility(View.VISIBLE);
            mHasSummaryContent = true;
        } else {
            mAbstract.setVisibility(View.GONE);
        }

        final View requirementsBlock = findViewById(R.id.session_requirements_block);
        final String sessionRequirements = cursor.getString(SessionsQuery.REQUIREMENTS);
        if (!TextUtils.isEmpty(sessionRequirements)) {
            UIUtils.setTextMaybeHtml(mRequirements, sessionRequirements);
            requirementsBlock.setVisibility(View.VISIBLE);
            mHasSummaryContent = true;
        } else {
            requirementsBlock.setVisibility(View.GONE);
        }

        setupModeratorTab(cursor);

        // Show empty message when all data is loaded, and nothing to show
        if (mSpeakersCursor && !mHasSummaryContent) {
            findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
        }
    } finally {
        cursor.close();
    }
}

From source file:at.bitfire.ical4android.AndroidTaskList.java

protected AndroidTask[] queryTasks(String where, String[] whereArgs) throws CalendarStorageException {
    where = (where == null ? "" : "(" + where + ") AND ") + Tasks.LIST_ID + "=?";
    whereArgs = ArrayUtils.add(whereArgs, String.valueOf(id));

    @Cleanup
    Cursor cursor = null;/*from www  . j  a va  2 s  .  co m*/
    try {
        cursor = provider.client.query(syncAdapterURI(provider.tasksUri()), taskBaseInfoColumns(), where,
                whereArgs, null);
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query calendar events", e);
    }

    List<AndroidTask> tasks = new LinkedList<>();
    while (cursor != null && cursor.moveToNext()) {
        ContentValues baseInfo = new ContentValues(cursor.getColumnCount());
        DatabaseUtils.cursorRowToContentValues(cursor, baseInfo);
        tasks.add(taskFactory.newInstance(this, cursor.getLong(0), baseInfo));
    }
    return tasks.toArray(taskFactory.newArray(tasks.size()));
}

From source file:com.example.bdcoe.sunshine.FetchWeatherTask.java

private long addLocation(String locationSetting, String cityName, double lat, double lon) {
    Log.v(LOG_TAG, "inserting " + cityName + ", with coord: " + lat + ", " + lon);

    // First, check if the location with this city name exists in the db
    Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);// w  w  w  .  j av a2 s.  c o m
    if (cursor.moveToFirst()) {
        Log.v(LOG_TAG, "Found it in the database!");
        int locationIdIndex = cursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        ContentValues locationValues = new ContentValues();
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        Uri locationInsertUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);

        return ContentUris.parseId(locationInsertUri);

    }
}