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.deliciousdroid.platform.ContactManager.java

/**
 * Returns the Data id for a sample SyncAdapter contact's profile row, or 0
 * if the sample SyncAdapter user isn't found.
 * /*w ww. j  ava2 s  .c o m*/
 * @param resolver a content resolver
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the profile Data row id, or 0 if not found
 */
private static long lookupProfile(ContentResolver resolver, String userName) {
    long profileId = 0;
    final Cursor c = resolver.query(Data.CONTENT_URI, ProfileQuery.PROJECTION, ProfileQuery.SELECTION,
            new String[] { userName }, null);
    try {
        if (c != null && c.moveToFirst()) {
            profileId = c.getLong(ProfileQuery.COLUMN_ID);
            Log.d("ProfileLookup", Long.toString(c.getLong(ProfileQuery.COLUMN_ID)));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return profileId;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap openPhoto(Context context, long contactId) {

    Log.e(TAG, "openPhoto");
    if (context == null) {
        return null;
    }/*from  w w  w .ja  va  2 s .c  om*/

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { ContactsContract.Contacts.Photo.PHOTO }, null, null, null);
    if (cursor == null) {
        return null;
    }

    try {
        if (cursor.moveToFirst()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                return BitmapFactory.decodeStream(new ByteArrayInputStream(data));
            }
        }
    } finally {
        cursor.close();
    }
    return null;
}

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

private static boolean isItemUpdated(Uri uri, JSONObject item, ContentResolver resolver) throws JSONException {
    final Cursor cursor = resolver.query(uri, MixItContract.Sessions.PROJ_DETAIL.PROJECTION, null, null, null);
    try {//from  w w w  . j a v  a2  s.c om
        if (!cursor.moveToFirst()) {
            return false;
        }

        String curTitle = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.TITLE);
        if (TextUtils.isEmpty(curTitle)) {
            curTitle = EMPTY;
        }

        String curSummary = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.SUMMARY);
        if (TextUtils.isEmpty(curSummary)) {
            curSummary = EMPTY;
        }

        String curDesc = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.DESC);
        if (TextUtils.isEmpty(curDesc)) {
            curDesc = EMPTY;
        }

        final long curStart = cursor.getLong(MixItContract.Sessions.PROJ_DETAIL.START);
        final long curEnd = cursor.getLong(MixItContract.Sessions.PROJ_DETAIL.END);

        String curRoomId = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.ROOM_ID);
        if (TextUtils.isEmpty(curRoomId)) {
            curRoomId = EMPTY;
        }

        final int curNbVotes = cursor.getInt(MixItContract.Sessions.PROJ_DETAIL.NB_VOTES);
        // final int curMyVote = cursor.getInt(MixItContract.Sessions.PROJ_DETAIL.MY_VOTE);
        // final int curIsFavorite = cursor.getInt(MixItContract.Sessions.PROJ_DETAIL.IS_FAVORITE);

        String curFormat = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.FORMAT);
        if (TextUtils.isEmpty(curFormat)) {
            curFormat = EMPTY;
        }

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

        String curLang = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.LANG);
        if (TextUtils.isEmpty(curLang)) {
            curLang = EMPTY;
        }

        final String newTitle = item.has(TAG_TITLE) ? item.getString(TAG_TITLE).trim() : curTitle;
        final String newSummary = item.has(TAG_SUMMARY) ? item.getString(TAG_SUMMARY).trim() : curSummary;
        final String newDesc = item.has(TAG_DESC) ? item.getString(TAG_DESC).trim() : curDesc;
        long newStart = 0;
        if (item.has(TAG_START)) {
            final String start = item.getString(TAG_START);
            newStart = DateUtils.parseISO8601(start);
        } else {
            newStart = curStart;
        }
        long newEnd = 0;
        if (item.has(TAG_END)) {
            final String end = item.getString(TAG_END);
            newEnd = DateUtils.parseISO8601(end);
        } else {
            newEnd = curEnd;
        }
        final String newRoomId = item.has(TAG_ROOM) ? item.getString(TAG_ROOM).trim() : curRoomId;
        final int newNbVotes = item.has(TAG_NB_VOTES) ? item.getInt(TAG_NB_VOTES) : curNbVotes;
        // final int newMyVote = session.has(TAG_MY_VOTE) ? session.getBoolean(TAG_MY_VOTE) ? 1 : 0 : curMyVote;
        // final int newIsFavorite = session.has(TAG_IS_FAVORITE) ? session.getInt(TAG_IS_FAVORITE) : curIsFavorite;
        final String newFormat = item.has(TAG_FORMAT) ? item.getString(TAG_FORMAT).trim() : curFormat;
        final String newLevel = item.has(TAG_LEVEL) ? item.getString(TAG_LEVEL).trim() : curLevel;
        final String newLang = item.has(TAG_LANG) ? item.getString(TAG_LANG).trim() : curLang;

        return !curTitle.equals(newTitle) || //
                !curSummary.equals(newSummary) || //
                !curDesc.equals(newDesc) || //
                curStart != newStart || //
                curEnd != newEnd || //
                !curRoomId.equals(newRoomId) || //
                curNbVotes != newNbVotes || //
                // curMyVote != newMyVote ||
                // curIsFavorite != newIsFavorite || //
                !curFormat.equals(newFormat) || //
                !curLevel.equals(newLevel) || //
                !curLang.equals(newLang);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.google.samples.apps.topeka.persistence.TopekaDatabaseHelper.java

/**
 * Gets all categories wrapped in a {@link Cursor} positioned at it's first element.
 * <p>There are <b>no quizzes</b> within the categories obtained from this cursor</p>
 *
 * @param context The context this is running in.
 * @return All categories stored in the database.
 *///from  w  w w.j  av  a 2  s.c  om
private static Cursor getCategoryCursor(Context context) {
    SQLiteDatabase readableDatabase = getReadableDatabase(context);
    Cursor data = readableDatabase.query(CategoryTable.NAME, CategoryTable.PROJECTION, null, null, null, null,
            null);
    data.moveToFirst();
    return data;
}

From source file:com.coinomi.wallet.ExchangeRatesProvider.java

@Nullable
public static ExchangeRate getRate(final Context context, @Nonnull final String coinSymbol,
        @Nonnull String localSymbol) {
    ExchangeRate rate = null;/* w  w  w  .  j av a  2  s  .  c o  m*/

    if (context != null) {
        final Uri uri = contentUriToCrypto(context.getPackageName(), localSymbol, true);
        final Cursor cursor = context.getContentResolver().query(uri, null,
                ExchangeRatesProvider.KEY_CURRENCY_ID, new String[] { coinSymbol }, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                rate = getExchangeRate(cursor);
            }
            cursor.close();
        }
    }

    return rate;
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Creates a {@link String} {@link List} from a {@link Cursor}
 *
 * @param cursor the input {@link Cursor}
 * @param name   the name of the colum//from   w w w  .  j  ava2  s.  c  o  m
 * @return a {@link String} {@link List}
 */
public static List<String> convertColumnToList(Cursor cursor, String name) {
    List<String> list = new ArrayList<>();
    if (!checkCursor(cursor)) {
        return null;
    }
    cursor.moveToFirst();
    do {
        list.add(cursor.getString(cursor.getColumnIndex(name)));
    } while (cursor.moveToNext());

    return list;
}

From source file:edu.mit.mobile.android.locast.data.TaggableItem.java

/**
 * TODO make this pick the set of tags for a set of content.
 *
 * @param cr a content resolver/*from w w w  .j  a v a2  s .co  m*/
 * @return the top MAX_POPULAR_TAGS most popular tags in the set, with the most popular first.
 */
public static List<String> getPopularTags(ContentResolver cr) {

    final Map<String, Integer> tagPop = new HashMap<String, Integer>();
    final List<String> popTags;

    final Cursor c = cr.query(Tag.CONTENT_URI, Tag.DEFAULT_PROJECTION, null, null, null);
    final int tagColumn = c.getColumnIndex(Tag._NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        final String tag = c.getString(tagColumn);

        final Integer count = tagPop.get(tag);
        if (count == null) {
            tagPop.put(tag, 1);
        } else {
            tagPop.put(tag, count + 1);
        }
    }
    c.close();

    popTags = new ArrayList<String>(tagPop.keySet());

    Collections.sort(popTags, new Comparator<String>() {
        public int compare(String object1, String object2) {
            return tagPop.get(object2).compareTo(tagPop.get(object1));
        }

    });
    int limit;
    if (popTags.size() < MAX_POPULAR_TAGS) {
        limit = popTags.size();
    } else {
        limit = MAX_POPULAR_TAGS;
    }
    return popTags.subList(0, limit);
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Creates a {@link List} from a {@link Cursor}
 *
 * @param <T>             the generic type of the {@link List}
 * @param cursor          the {@link Cursor} to be converted to a {@link List}
 * @param databaseAdapter the {@link DatabaseAdapter} that will be used for conversion
 * @return a {@link List} containing objects created from the input
 * {@link Cursor}// w  w  w.  java2s. c o  m
 */
public static <T> List<T> convertCursorToList(Cursor cursor, DatabaseAdapter<T> databaseAdapter) {
    List<T> list = new ArrayList<>();
    if (!checkCursor(cursor)) {
        return null;
    }
    cursor.moveToFirst();
    do {
        list.add(databaseAdapter.deserialize(cursor));
    } while (cursor.moveToNext());
    return list;
}

From source file:com.hivewallet.androidclient.wallet.AddressBookProvider.java

public static AddressBookEntry lookupEntry(final Context context, @Nonnull final String address) {
    String label = null;/*  w w w.  java 2 s.  c om*/
    String photo = null;

    final Uri uri = contentUri(context.getPackageName()).buildUpon().appendPath(address).build();
    final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            label = cursor.getString(cursor.getColumnIndexOrThrow(KEY_LABEL));
            photo = cursor.getString(cursor.getColumnIndexOrThrow(KEY_PHOTO));
        }

        cursor.close();
    }

    if (label != null) {
        return new AddressBookEntry(address, label, photo);
    } else {
        return null;
    }
}

From source file:de.wikilab.android.friendica01.Max.java

public static String getRealPathFromURI(Context ctx, Uri contentUri) {
    Log.i("FileTypes", "URI = " + contentUri + ", scheme = " + contentUri.getScheme());
    if (contentUri.getScheme().equals("file")) {
        return contentUri.getPath();
    } else {/* w  ww .j  a va 2s . c  om*/
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String res = cursor.getString(column_index);
        cursor.close();
        return res;
    }
}