Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

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

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:com.karura.framework.plugins.Capture.java

/**
 * Queries the media store to find out what the file path is for the Uri we supply
 * /*from  w  w w .jav a2 s .com*/
 * @param contentUri
 *            the Uri of the audio/image/video
 * @param context
 *            the current application context
 * @return the full path to the file
 */
@SuppressWarnings("deprecation")
public static String getRealPathFromURI(Uri contentUri, Activity context) {
    final String scheme = contentUri.getScheme();

    if (scheme == null) {
        return contentUri.toString();
    } else if (scheme.compareTo("content") == 0) {
        String[] proj = { DATA };
        Cursor cursor = context.managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else if (scheme.compareTo("file") == 0) {
        return contentUri.getPath();
    } else {
        return contentUri.toString();
    }
}

From source file:org.jsharkey.sky.WebserviceHelper.java

/**
 * Perform a webservice query to retrieve and store the forecast for the
 * given widget. This call blocks until request is finished and
 * {@link Forecasts#CONTENT_URI} has been updated.
 *//*  w ww  .  j a v  a2s.  c om*/
public static void updateForecasts(Context context, Uri appWidgetUri, int days) throws ForecastParseException {

    Uri appWidgetForecasts = Uri.withAppendedPath(appWidgetUri, AppWidgets.TWIG_FORECASTS);

    ContentResolver resolver = context.getContentResolver();

    Cursor cursor = null;
    double lat = Double.NaN;
    double lon = Double.NaN;

    // Pull exact forecast location from database
    try {
        cursor = resolver.query(appWidgetUri, PROJECTION_APPWIDGET, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            lat = cursor.getDouble(COL_LAT);
            lon = cursor.getDouble(COL_LON);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    // Query webservice for this location
    List<Forecast> forecasts = queryLocation(lat, lon, days);

    if (forecasts == null || forecasts.size() == 0) {
        throw new ForecastParseException("No forecasts found from webservice query");
    }

    // Purge existing forecasts covered by incoming data, and anything
    // before today
    long lastMidnight = ForecastUtils.getLastMidnight();
    long earliest = Long.MAX_VALUE;
    for (Forecast forecast : forecasts) {
        earliest = Math.min(earliest, forecast.validStart);
    }

    resolver.delete(appWidgetForecasts, ForecastsColumns.VALID_START + " >= " + earliest + " OR "
            + ForecastsColumns.VALID_START + " <= " + lastMidnight, null);

    // Insert any new forecasts found
    ContentValues values = new ContentValues();
    for (Forecast forecast : forecasts) {
        Log.d(TAG, "inserting forecast with validStart=" + forecast.validStart);
        values.clear();
        values.put(ForecastsColumns.VALID_START, forecast.validStart);
        values.put(ForecastsColumns.TEMP_HIGH, forecast.tempHigh);
        values.put(ForecastsColumns.TEMP_LOW, forecast.tempLow);
        values.put(ForecastsColumns.CONDITIONS, forecast.conditions);
        values.put(ForecastsColumns.URL, forecast.url);
        if (forecast.alert) {
            values.put(ForecastsColumns.ALERT, ForecastsColumns.ALERT_TRUE);
        }
        resolver.insert(appWidgetForecasts, values);
    }

    // Mark widget cache as being updated
    values.clear();
    values.put(AppWidgetsColumns.LAST_UPDATED, System.currentTimeMillis());
    resolver.update(appWidgetUri, values, null, null);
}

From source file:foam.zizim.android.BoskoiService.java

public static BlogData getBlogData(int id) {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchBlogById(id);
    BlogData blogData = null;/* w w  w. ja  v  a  2  s .  com*/

    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int descIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DESCRIPTION);

        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {

            blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setDescription(cursor.getString(descIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blogData;
}

From source file:fr.mixit.android.io.JsonHandlerApplyMembers.java

private static boolean isItemUpdated(Uri uri, JSONObject item, ContentResolver resolver, int newMemberType)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, MixItContract.Members.PROJ_DETAIL.PROJECTION, null, null, null);
    try {/*from  w  w w  .  j  ava  2  s .  c  o m*/
        if (!cursor.moveToFirst()) {
            return false;
        }

        String curFirstName = cursor.getString(MixItContract.Members.PROJ_DETAIL.FIRSTNAME);
        if (TextUtils.isEmpty(curFirstName)) {
            curFirstName = EMPTY;
        }
        String curLastName = cursor.getString(MixItContract.Members.PROJ_DETAIL.LASTNAME);
        if (TextUtils.isEmpty(curLastName)) {
            curLastName = EMPTY;
        }

        String curLogin = cursor.getString(MixItContract.Members.PROJ_DETAIL.LOGIN);
        if (TextUtils.isEmpty(curLogin)) {
            curLogin = EMPTY;
        }

        String curCompany = cursor.getString(MixItContract.Members.PROJ_DETAIL.COMPANY);
        if (TextUtils.isEmpty(curCompany)) {
            curCompany = EMPTY;
        }

        String curShortDesc = cursor.getString(MixItContract.Members.PROJ_DETAIL.SHORT_DESC);
        if (TextUtils.isEmpty(curShortDesc)) {
            curShortDesc = EMPTY;
        }

        String curLongDesc = cursor.getString(MixItContract.Members.PROJ_DETAIL.LONG_DESC);
        if (TextUtils.isEmpty(curLongDesc)) {
            curLongDesc = EMPTY;
        }

        String curImageUrl = cursor.getString(MixItContract.Members.PROJ_DETAIL.IMAGE_URL);
        if (TextUtils.isEmpty(curImageUrl)) {
            curImageUrl = EMPTY;
        }

        final int curNbConsults = cursor.getInt(MixItContract.Members.PROJ_DETAIL.NB_CONSULT);

        String curLevel = cursor.getString(MixItContract.Members.PROJ_DETAIL.LEVEL);
        if (TextUtils.isEmpty(curLevel)) {
            curLevel = EMPTY;
        }

        // final int curMemberType = cursor.getInt(MixItContract.Members.PROJ_DETAIL.TYPE);

        final String newFirstName = item.has(TAG_FIRSTNAME) ? item.getString(TAG_FIRSTNAME).trim()
                : curFirstName;
        final String newLastName = item.has(TAG_LASTNAME) ? item.getString(TAG_LASTNAME).trim() : curLastName;
        final String newLogin = item.has(TAG_LOGIN) ? item.getString(TAG_LOGIN).trim() : curLogin;
        final String newCompany = item.has(TAG_COMPANY) ? item.getString(TAG_COMPANY).trim() : curCompany;
        final String newShortDesc = item.has(TAG_SHORT_DESC) ? item.getString(TAG_SHORT_DESC).trim()
                : curShortDesc;
        final String newLongDesc = item.has(TAG_LONG_DESC) ? item.getString(TAG_LONG_DESC).trim() : curLongDesc;
        final String newImageUrl = item.has(TAG_LOGO) ? item.getString(TAG_LOGO).trim()
                : item.has(TAG_IMAGE_URL) ? item.getString(TAG_IMAGE_URL).trim() : curImageUrl;
        final int newNbConsults = item.has(TAG_NB_CONSULTS) ? item.getInt(TAG_NB_CONSULTS) : curNbConsults;
        final String newLevel = item.has(TAG_LEVEL) ? item.getString(TAG_LEVEL).trim() : curLevel;

        return !curFirstName.equalsIgnoreCase(newFirstName) || //
                !curLastName.equalsIgnoreCase(newLastName) || //
                !curLogin.equals(newLogin) || //
                !curCompany.equals(newCompany) || //
                !curShortDesc.equals(newShortDesc) || //
                !curLongDesc.equals(newLongDesc) || //
                !curImageUrl.equals(newImageUrl) || //
                curNbConsults != newNbConsults || //
                !curLevel.equals(newLevel)
        /* || // curMemberType != newMemberType && newMemberType != MixItContract.Members.TYPE_MEMBER */;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

public static String getRealPathFromURI(Context mContext, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }/*  ww  w. ja v a2 s .  c o  m*/
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String path = cursor.getString(column_index);
    cursor.close();
    return path;
}

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

public static Contact forceGetContact(Context context, long contactId) {
    if (contactId == Contact.MY_ID) {
        DBHelper dbh = new DBHelper(context);
        DBIdentityProvider idp = new DBIdentityProvider(dbh);
        try {/*from   w  w w . j  a  v a  2s  . c om*/
            return idp.contactForUser();
        } finally {
            idp.close();
            dbh.close();
        }
    }
    Cursor c = context.getContentResolver().query(
            Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts"), null, Contact._ID + "=?",
            new String[] { String.valueOf(contactId) }, null);
    if (c == null)
        return null;
    try {

        if (!c.moveToFirst()) {
            return null;
        } else {
            return new Contact(c);
        }
    } finally {
        c.close();
    }
}

From source file:foam.zizim.android.BoskoiService.java

public static List<BlogData> getBlogData() {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchAllBlog();
    List blog = new ArrayList<BlogData>();
    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int descIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DESCRIPTION);

        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {/*from   w  ww.  j ava  2s.  c o  m*/

            BlogData blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setDescription(cursor.getString(descIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));
            blog.add(blogData);

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blog;
}

From source file:foam.zizim.android.BoskoiService.java

public static List<BlogData> getSimpleBlogData() {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchAllSimpleBlog();
    List blog = new ArrayList<BlogData>();
    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {/*  w  w  w  .  j a  v  a  2s  .co  m*/

            BlogData blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));
            blog.add(blogData);

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blog;
}

From source file:info.staticfree.android.units.UnitUsageDBHelper.java

/**
 * Increments the usage counter for the given unit.
 * @param unit name of the unit/*  w w w . j  a  v a2  s .  c  o m*/
 * @param db the unit usage database
 */
public static void logUnitUsed(String unit, ContentResolver cr) {
    final String[] selectionArgs = { unit };
    final Cursor c = cr.query(UsageEntry.CONTENT_URI, INCREMENT_QUERY_PROJECTION, UsageEntry._UNIT + "=?",
            selectionArgs, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        final int useCount = c.getInt(c.getColumnIndex(UsageEntry._USE_COUNT));
        final int id = c.getInt(c.getColumnIndex(UsageEntry._ID));
        final ContentValues cv = new ContentValues();
        cv.put(UsageEntry._USE_COUNT, useCount + 1);

        cr.update(ContentUris.withAppendedId(UsageEntry.CONTENT_URI, id), cv, null, null);
    } else {
        final ContentValues cv = new ContentValues();
        cv.put(UsageEntry._UNIT, unit);
        cv.put(UsageEntry._USE_COUNT, 1);
        cv.put(UsageEntry._FACTOR_FPRINT, getFingerprint(unit));
        cr.insert(UsageEntry.CONTENT_URI, cv);
    }
    c.close();
}

From source file:it.feio.android.omninotes.utils.StorageHelper.java

public static String getRealPathFromURI(Context mContext, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }//from  ww  w  .  j  a  v  a2 s.c o  m
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}