Example usage for android.database Cursor moveToPosition

List of usage examples for android.database Cursor moveToPosition

Introduction

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

Prototype

boolean moveToPosition(int position);

Source Link

Document

Move the cursor to an absolute position.

Usage

From source file:cm.confide.ex.chips.RecipientAlternatesAdapter.java

/**
 * @return a new cursor based on the given cursor with all duplicate destinations removed.
 *
 * It's only intended to use for the alternate list, so...
 * - This method ignores all other fields and dedupe solely on the destination.  Normally,
 * if a cursor contains multiple contacts and they have the same destination, we'd still want
 * to show both.//from   w  ww . j a v  a  2 s  .  c  o m
 * - This method creates a MatrixCursor, so all data will be kept in memory.  We wouldn't want
 * to do this if the original cursor is large, but it's okay here because the alternate list
 * won't be that big.
 */
// Visible for testing
/* package */ static Cursor removeDuplicateDestinations(Cursor original) {
    final MatrixCursor result = new MatrixCursor(original.getColumnNames(), original.getCount());
    final HashSet<String> destinationsSeen = new HashSet<String>();

    original.moveToPosition(-1);
    while (original.moveToNext()) {
        final String destination = original.getString(Query.DESTINATION);
        if (destinationsSeen.contains(destination)) {
            continue;
        }
        destinationsSeen.add(destination);

        result.addRow(new Object[] { original.getString(Query.NAME), original.getString(Query.DESTINATION),
                original.getInt(Query.DESTINATION_TYPE), original.getString(Query.DESTINATION_LABEL),
                original.getLong(Query.CONTACT_ID), original.getLong(Query.DATA_ID),
                original.getString(Query.PHOTO_THUMBNAIL_URI), original.getInt(Query.DISPLAY_NAME_SOURCE) });
    }

    return result;
}

From source file:ch.blinkenlights.android.vanilla.MediaUtils.java

/**
 * Returns the first matching song (or NULL) of given type + id combination
 *
 * @param context A Context to use.//w  w  w.ja  v a  2s.  c  o  m
 * @param type The MediaTye to query
 * @param id The id of given type to query
 */
public static Song getSongByTypeId(Context context, int type, long id) {
    Song song = new Song(-1);
    QueryTask query = buildQuery(type, id, Song.FILLED_PROJECTION, null);
    Cursor cursor = query.runQuery(context);
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            cursor.moveToPosition(0);
            song.populate(cursor);
        }
        cursor.close();
    }
    return song.isFilled() ? song : null;
}

From source file:mobisocial.musubi.ImageGalleryActivity.java

private static int binarySearch(Cursor c, long id, int colId) {
    long test;/*w  ww .j  a  va 2 s.c o m*/
    int first = 0;
    int max = c.getCount();
    while (first < max) {
        int mid = (first + max) / 2;
        c.moveToPosition(mid);
        test = c.getLong(colId);
        if (id < test) {
            max = mid;
        } else if (id > test) {
            first = mid + 1;
        } else {
            return mid;
        }
    }
    return 0;
}

From source file:com.rp.justcast.video.VideoProvider.java

public static List<MediaInfo> buildMedia() throws JSONException {

    if (null != mediaList) {
        return mediaList;
    }/*from  ww  w .  ja  v a  2s.  co  m*/

    String[] columns = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
            MediaStore.Video.Media.DURATION };
    String orderBy = MediaStore.Images.Media.DATE_TAKEN + " desc";
    Cursor videoCursor = null;
    try {
        videoCursor = JustCast.getmAppContext().getContentResolver()
                .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        videoCursor.moveToFirst();
        long fileId = videoCursor.getLong(videoCursor.getColumnIndex(MediaStore.Video.Media._ID));
        Log.w(TAG, "Building Media");
        Log.w(TAG, "Video Count" + videoCursor.getCount());
        int count = videoCursor.getCount();
        Log.d(TAG, "Count of images" + count);
        mediaList = new ArrayList<MediaInfo>();
        for (int i = 0; i < count; i++) {
            videoCursor.moveToPosition(i);
            int dataColumnIndex = videoCursor.getColumnIndex(MediaStore.Video.Media.DATA);
            int titleIndex = videoCursor.getColumnIndex(MediaStore.Video.Media.TITLE);
            Log.w(TAG, "Video added" + videoCursor.getString(dataColumnIndex));
            String path = videoCursor.getString(dataColumnIndex);
            String title = videoCursor.getString(titleIndex);
            MediaMetadata movieMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
            movieMetadata.putString("VIDEO_PATH", path);
            movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, title);
            movieMetadata.putString(MediaMetadata.KEY_TITLE, title);
            movieMetadata.putString(MediaMetadata.KEY_STUDIO, title);
            path = JustCast.addJustCastServerParam(path);
            MediaInfo mediaInfo = new MediaInfo.Builder(path).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                    .setContentType(getMediaType()).setMetadata(movieMetadata).build();
            mediaList.add(mediaInfo);
        }
    } finally {
        videoCursor.close();
    }
    return mediaList;
}

From source file:com.rsegismont.androlife.core.utils.AndrolifeUtils.java

public static long getTonightIndex(Cursor mCursor) {
    if (mCursor == null)
        return -1;

    if (mCursor.getCount() <= 0)
        return -1;

    long dateUtc = -1;

    final int initialPosition = mCursor.getPosition();

    mCursor.moveToPosition(0);

    final long initialValue = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));

    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(initialValue);
    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    for (int i = 0; i < mCursor.getCount(); i++) {
        mCursor.moveToPosition(i);/*from  ww w  . j  a  va2  s .  com*/
        final long value = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));

        if (calendar.getTime().getTime() - value < 0) {

            mCursor.moveToPosition(Math.max(0, i - 1));
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }
        if ((i == 0) && (calendar.getTime().getTime() - value < 0)) {

            mCursor.moveToPosition(0);
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }

        if (i == (mCursor.getCount() - 1)) {

            mCursor.moveToPosition(Math.max(0, mCursor.getCount() - 1));
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }
    }

    if (initialPosition >= 0)
        mCursor.moveToPosition(initialPosition);

    return dateUtc;
}

From source file:com.android.calendar.event.EventLocationAdapter.java

/**
 * Matches the input string against contacts names and addresses.
 *
 * @param resolver        The content resolver.
 * @param input           The user-typed input string.
 * @param addressesRetVal The addresses in the returned result are also returned here
 *                        for faster lookup.  Pass in an empty set.
 * @return Ordered list of all the matched results.  If there are multiple address matches
 * for the same contact, they will be listed together in individual items, with only
 * the first item containing a name/icon.
 *///from w  ww  .ja v  a  2 s .c o m
private static List<Result> queryContacts(ContentResolver resolver, String input,
        HashSet<String> addressesRetVal) {
    String where = null;
    String[] whereArgs = null;

    // Match any word in contact name or address.
    if (!TextUtils.isEmpty(input)) {
        where = CONTACTS_WHERE;
        String param1 = input + "%";
        String param2 = "% " + input + "%";
        whereArgs = new String[] { param1, param2, param1, param2 };
    }

    // Perform the query.
    Cursor c = resolver.query(CommonDataKinds.StructuredPostal.CONTENT_URI, CONTACTS_PROJECTION, where,
            whereArgs, Contacts.DISPLAY_NAME + " ASC");

    // Process results.  Group together addresses for the same contact.
    try {
        Map<String, List<Result>> nameToAddresses = new HashMap<String, List<Result>>();
        c.moveToPosition(-1);
        while (c.moveToNext()) {
            String name = c.getString(CONTACTS_INDEX_DISPLAY_NAME);
            String address = c.getString(CONTACTS_INDEX_ADDRESS);
            if (name != null) {

                List<Result> addressesForName = nameToAddresses.get(name);
                Result result;
                if (addressesForName == null) {
                    // Determine if there is a photo for the icon.
                    Uri contactPhotoUri = null;
                    if (c.getLong(CONTACTS_INDEX_PHOTO_ID) > 0) {
                        contactPhotoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
                                c.getLong(CONTACTS_INDEX_CONTACT_ID));
                    }

                    // First listing for a distinct contact should have the name/icon.
                    addressesForName = new ArrayList<Result>();
                    nameToAddresses.put(name, addressesForName);
                    result = new Result(name, address, R.drawable.ic_contact_picture, contactPhotoUri);
                } else {
                    // Do not include name/icon in subsequent listings for the same contact.
                    result = new Result(null, address, null, null);
                }

                addressesForName.add(result);
                addressesRetVal.add(address);
            }
        }

        // Return the list of results.
        List<Result> allResults = new ArrayList<Result>();
        for (List<Result> result : nameToAddresses.values()) {
            allResults.addAll(result);
        }
        return allResults;

    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:com.rsegismont.androlife.core.utils.AndrolifeUtils.java

public static long getCurrentIndex(Cursor mCursor) {

    long dateUtc = -1;

    if (mCursor == null)
        return -1;

    if (mCursor.getCount() <= 0)
        return -1;

    final int initialPosition = mCursor.getPosition();

    int index = -1;

    for (int i = 0; i < mCursor.getCount(); i++) {
        mCursor.moveToPosition(i);
        final long value = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
        if (System.currentTimeMillis() - value < 0) {
            index = i - 1;// w  w  w  . j a  v  a2  s  .  c  o m
            mCursor.moveToPosition(Math.max(0, index));
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }

        if ((i == 0) && (System.currentTimeMillis() - value < 0)) {

            mCursor.moveToPosition(0);
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }

        if (i == (mCursor.getCount() - 1)) {
            index = (mCursor.getCount() - 1);
            mCursor.moveToPosition(Math.max(0, index));
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }
    }

    if (initialPosition >= 0)
        mCursor.moveToPosition(initialPosition);
    return dateUtc;

}

From source file:com.zns.comicdroid.adapter.GroupedItemAdapter.java

public String getGroupedItemName(int position) {
    Cursor cursor = getCursor();
    if (cursor.moveToPosition(position))
        return cursor.getString(1);
    return null;/*from  w w w  .  j  a va2 s.c  om*/
}

From source file:org.mariotaku.twidere.adapter.TrendsAdapter.java

@Override
public String getItem(int position) {
    final Cursor c = getCursor();
    if (c != null && !c.isClosed() && c.moveToPosition(position))
        return c.getString(mNameIdx);
    return null;/* w w w .  j av a  2  s .c om*/
}

From source file:whipkey.stemesteem.components.StemListFragment.java

public void onListItemClick(ListView l, View v, int position, long id) {
    SimpleCursorAdapter adapter = (SimpleCursorAdapter) l.getAdapter();
    Cursor c = adapter.getCursor();

    c.moveToPosition(position);
    int weekNo = c.getInt(c.getColumnIndex("weekNo"));
    int stemNo = c.getInt(c.getColumnIndex("stemNo"));
    String stem = c.getString(c.getColumnIndex("stem"));
    clickListener.stemListItemClick(weekNo, stemNo, stem); // call back to
    // our activity,
    // requesting
    // the//from   w  ww  .ja  v  a 2  s.c o m
    // currentWeek's
    // value so that
    // our listview
    // can be
    // updated.
}