Java Code Examples for android.database.Cursor#moveToPosition()

The following examples show how to use android.database.Cursor#moveToPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchView.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * Query rewriting.
 */
private void rewriteQueryFromSuggestion(int position) {
    CharSequence oldQuery = mQueryTextView.getText();
    Cursor c = mSuggestionsAdapter.getCursor();
    if (c == null) {
        return;
    }
    if (c.moveToPosition(position)) {
        // Get the new query from the suggestion.
        CharSequence newQuery = mSuggestionsAdapter.convertToString(c);
        if (newQuery != null) {
            // The suggestion rewrites the query.
            // Update the text field, without getting new suggestions.
            setQuery(newQuery);
        } else {
            // The suggestion does not rewrite the query, restore the user's query.
            setQuery(oldQuery);
        }
    } else {
        // We got a bad position, restore the user's query.
        setQuery(oldQuery);
    }
}
 
Example 2
Source File: ContactLoadingActivity.java    From chips-input-layout with MIT License 6 votes vote down vote up
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Collect all the contact information from the Cursor in a List
    List<ContactChip> chips = new ArrayList<>(data.getCount());
    for (int i = 0; i < data.getCount(); i++) {
        data.moveToPosition(i);

        ContactChip chip = new ContactChip();
        chip.setId(i);
        chip.setName(data.getString(data.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)));
        chip.setPhone(data.getString(data.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        int phoneType = data.getInt(data.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
        chip.setPhoneType(ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), phoneType, "").toString());

        String avatar = data.getString(data.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
        if (avatar != null) {
            chip.setAvatarUri(Uri.parse(avatar));
        }

        chips.add(chip);
    }

    // Set the list of chips on the ChipsInput and normal adapter
    onContactsAvailable(chips);
}
 
Example 3
Source File: AppUtils.java    From android with Apache License 2.0 6 votes vote down vote up
public static String getFavoriteLink(Context context, int position) {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String sortOrder = prefs.getString(Config.SORT_ORDER_FAVORITE, Config.SORT_FAVORITE_DEFAULT);

        final ContentResolver resolver = context.getApplicationContext().getContentResolver();
        final String[] cols = new String[]{FavoritesTable.COLUMN_LINK};
        final Uri favouritesUri = FavoritesContentProvider.CONTENT_URI;
        final Cursor cursor = resolver.query(favouritesUri, cols, null, null, sortOrder);
        if (cursor != null && cursor.moveToPosition(position)) {
            String link = cursor.getString(cursor.getColumnIndex(FavoritesTable.COLUMN_LINK));
            cursor.close();
            return link;
        }
        return null;
    }
 
Example 4
Source File: ItemShoppingListCursorAdapter.java    From ShoppingList with MIT License 6 votes vote down vote up
public String[] getDescriptions() {
    Cursor c = getCursor();
    if (c != null) {
        String[] descriptions = new String[c.getCount()];

        for (int i = 0; i < c.getCount(); i++) {
            c.moveToPosition(i);
            ItemShoppingList itemShoppingList = getItem(c.getPosition());

            descriptions[c.getPosition()] = itemShoppingList.getDescription();
        }

        return descriptions;
    }

    return new String[]{};
}
 
Example 5
Source File: MusicFinder.java    From Flute-Music-Player with Apache License 2.0 6 votes vote down vote up
public String getURI() {

            Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID};
            String selection = MediaStore.Audio.Media._ID + "=?";
            String[] selectionArgs = new String[] {"" + id}; //This is the id you are looking for

            Cursor mediaCursor = getContentResolver().query(mediaContentUri, projection, selection, selectionArgs, null);

            if(mediaCursor.getCount() >= 0) {
                mediaCursor.moveToPosition(0);
//                String title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//                String album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
//                String artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
//                long duration = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                uri = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                //Do something with the data
            }
            mediaCursor.close();
            return uri;

        }
 
Example 6
Source File: ConnectionListFragment.java    From MongoExplorer with MIT License 6 votes vote down vote up
private void selectItem(Cursor cursor, long id) {
	int pos = 0;
	int original = cursor.getPosition();
	if (!cursor.moveToFirst())
		return;

	do {
		if (cursor.getLong(MongoBrowserProvider.INDEX_CONNECTION_ID) == id)
			break;
		pos++;
	} while (cursor.moveToNext());
	
	cursor.moveToPosition(original);

       setActivatedPosition(pos);
}
 
Example 7
Source File: TestActivity.java    From v2ex with Apache License 2.0 6 votes vote down vote up
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    onRefreshingStateChanged(false);
    if (data != null) {
        data.moveToPosition(-1);
        if (data.moveToFirst()) {
            String id = data.getString(data.getColumnIndex(V2exContract.Reviews.REVIEW_ID));
            String member = data.getString(data.getColumnIndex(V2exContract.Reviews.REVIEW_MEMBER));
            String content = data.getString(data.getColumnIndex(V2exContract.Reviews.REVIEW_CONTENT));
            int reviewCount = data.getCount();
            Toast.makeText(TestActivity.this, "review_id=" + id +
                            ", reviewMember=" + member +
                            ", reviewContent=" + content +
                            ", reviewCount=" + reviewCount,
                    Toast.LENGTH_SHORT).show();
            LOGD(TAG, "review_id=" + id +
                    ", reviewMember=" + member +
                    ", reviewContent=" + content +
                    ", reviewCount=" + reviewCount);

        }
    }
}
 
Example 8
Source File: PredatorDbValuesHelper.java    From Capstone-Project with MIT License 5 votes vote down vote up
public static List<Post> getPostsFromCursor(Cursor cursor) {
    List<Post> posts = new ArrayList<>();
    for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        Post post = new Post();
        post.setId(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_ID));
        post.setPostId(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_POST_ID));
        post.setCategoryId(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_CATEGORY_ID));
        post.setDay(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_DAY));
        post.setName(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_NAME));
        post.setTagline(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_TAGLINE));
        post.setCommentCount(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_COMMENT_COUNT));
        post.setCreatedAt(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_CREATED_AT));
        post.setCreatedAtMillis(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_CREATED_AT_MILLIS));
        post.setDiscussionUrl(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_DISCUSSION_URL));
        post.setRedirectUrl(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_REDIRECT_URL));
        post.setVotesCount(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_VOTES_COUNT));
        post.setThumbnailImageUrl(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_THUMBNAIL_IMAGE_URL));
        post.setThumbnailImageUrlOriginal(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_THUMBNAIL_IMAGE_URL_ORIGINAL));
        post.setScreenshotUrl300px(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_SCREENSHOT_URL_300PX));
        post.setScreenshotUrl850px(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_SCREENSHOT_URL_850PX));
        post.setUsername(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_USER_NAME));
        post.setUsernameAlternative(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_USER_USERNAME));
        post.setUserId(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_USER_ID));
        post.setUserImageUrl100px(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_USER_IMAGE_URL_100PX));
        post.setUserImageUrlOriginal(CursorUtils.getString(cursor, PredatorContract.PostsEntry.COLUMN_USER_IMAGE_URL_ORIGINAL));
        post.setNotificationShown(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_NOTIFICATION_SHOWN));
        post.setRead(CursorUtils.getInt(cursor, PredatorContract.PostsEntry.COLUMN_READ_STATUS));
        posts.add(post);
    }

    return posts;
}
 
Example 9
Source File: AlbumsRecyclerAdapter.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
private void onItemClick(final int position) {
    final Cursor item = getCursor();
    if (item != null && item.moveToPosition(position)) {
        onAlbumClick(position,
                item.getLong(AlbumsProviderKt.COLUMN_ID),
                item.getString(AlbumsProviderKt.COLUMN_ALBUM));
    }
}
 
Example 10
Source File: SectionCursorAdapter.java    From SectionCursorAdapter with Apache License 2.0 5 votes vote down vote up
/**
 * If the adapter's cursor is not null then this method will call buildSections(Cursor cursor).
 */
private void buildSections() {
    if (hasOpenCursor()) {
        Cursor cursor = getCursor();
        cursor.moveToPosition(-1);
        mSections = buildSections(cursor);
        if (mSections == null) {
            mSections = new TreeMap<Integer, Object>();
        }
    }
}
 
Example 11
Source File: Utils.java    From Android-RecurrencePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Scan through a cursor of calendars and check if names are duplicated.
 * This travels a cursor containing calendar display names and fills in the
 * provided map with whether or not each name is repeated.
 *
 * @param isDuplicateName The map to put the duplicate check results in.
 * @param cursor          The query of calendars to check
 * @param nameIndex       The column of the query that contains the display name
 */
public static void checkForDuplicateNames(
        Map<String, Boolean> isDuplicateName, Cursor cursor, int nameIndex) {
    isDuplicateName.clear();
    cursor.moveToPosition(-1);
    while (cursor.moveToNext()) {
        String displayName = cursor.getString(nameIndex);
        // Set it to true if we've seen this name before, false otherwise
        if (displayName != null) {
            isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName));
        }
    }
}
 
Example 12
Source File: CursorToItemConverter.java    From PhilHackerNews with MIT License 5 votes vote down vote up
@Override
public List<Item> convertData(Cursor data) {
    List<Item> itemIds = new ArrayList<>(data.getCount());
    //Ensure cursor is positioned before its first row
    data.moveToPosition(-1);
    while (data.moveToNext()) {
        int id = data.getInt(ID_COL_POS);
        String type = data.getString(TYPE_COL_POS);
        int score = data.getInt(SCORE_COL_POS);
        String title = data.getString(TITLE_COL_POS);
        String author = data.getString(AUTHOR_COL_POS);
        String url = data.getString(URL_POS);
        String text = data.getString(TEXT_POS);
        String commentsString = data.getString(COMMENTS_POS);
        int parent = data.getInt(PARENT_POS);
        String[] commentIdStrings = commentsString.split(",");
        int[] commentIds = new int[commentIdStrings.length];
        for (int i=0, end=commentIdStrings.length; i<end; i++) {
            if (!TextUtils.isEmpty(commentIdStrings[i])) {
                commentIds[i] = Integer.parseInt(commentIdStrings[i]);
            }
        }
        boolean deleted = data.getInt(DELETED_POS) == 1;
        //noinspection ResourceType - type received from database, so we assume valid value
        Item item = new Item(id, type, score, title, author, url, text, commentIds, parent, deleted);
        itemIds.add(item);
    }
    return itemIds;
}
 
Example 13
Source File: ConversationListFragment.java    From zom-android-matrix with Apache License 2.0 5 votes vote down vote up
public long getItemId (int position)
{
    Cursor c = getCursor();
    c.moveToPosition(position);
    long chatId = c.getLong(ConversationListItem.COLUMN_CONTACT_ID);
    return chatId;
}
 
Example 14
Source File: CompositeCursorAdapter.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the item ID for the specified list position.
 */
public long getItemId(int position) {
    ensureCacheValid();
    int start = 0;
    for (Partition mPartition : mPartitions) {
        int end = start + mPartition.count;
        if (position >= start && position < end) {
            int offset = position - start;
            if (mPartition.hasHeader) {
                offset--;
            }
            if (offset == -1) {
                return 0;
            }
            if (mPartition.idColumnIndex == -1) {
                return 0;
            }

            Cursor cursor = mPartition.cursor;
            if (cursor == null || cursor.isClosed() || !cursor.moveToPosition(offset)) {
                return 0;
            }
            return cursor.getLong(mPartition.idColumnIndex);
        }
        start = end;
    }

    return 0;
}
 
Example 15
Source File: BaseImageList.java    From droidddle with Apache License 2.0 5 votes vote down vote up
public IImage getImageAt(int i) {
    BaseImage result = mCache.get(i);
    if (result == null) {
        Cursor cursor = getCursor();
        if (cursor == null)
            return null;
        synchronized (this) {
            result = cursor.moveToPosition(i) ? loadImageFromCursor(cursor) : null;
            mCache.put(i, result);
        }
    }
    return result;
}
 
Example 16
Source File: TracksRecyclerAdapter.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public String getSectionText(final int position) {
    final Cursor c = getCursor();
    if (c != null && c.moveToPosition(position)) {
        final String title = c.getString(TracksProviderKt.COLUMN_TITLE);
        if (!TextUtils.isEmpty(title)) {
            return String.valueOf(title.charAt(0));
        }
    }
    return null;
}
 
Example 17
Source File: RecipientAlternatesAdapter.java    From ChipsLibrary with Apache License 2.0 5 votes vote down vote up
public RecipientEntry getRecipientEntry(final int position)
{
final Cursor c=getCursor();
c.moveToPosition(position);
return RecipientEntry.constructTopLevelEntry(c.getString(Queries.Query.NAME),c.getInt(Queries.Query.DISPLAY_NAME_SOURCE),c.getString(Queries.Query.DESTINATION),c.getInt(Queries.Query.DESTINATION_TYPE),c.getString(Queries.Query.DESTINATION_LABEL),c.getLong(Queries.Query.CONTACT_ID),c.getLong(Queries.Query.DATA_ID),c.getString(Queries.Query.PHOTO_THUMBNAIL_URI),true,false /*
                                                                                                                                                                                                                                                                                                                                                                                      * isGalContact TODO(skennedy) We should
                                                                                                                                                                                                                                                                                                                                                                                      * look these up eventually
                                                                                                                                                                                                                                                                                                                                                                                      */);
}
 
Example 18
Source File: AppUtils.java    From android with Apache License 2.0 5 votes vote down vote up
public static String getRecentEpisode(Context context, int position) {

        final ContentResolver resolver = context.getContentResolver();
        final String[] cols = new String[]{RecentTable.COLUMN_EPISODE};
        final Uri recentUri = RecentContentProvider.CONTENT_URI;
        final Cursor cursor = resolver.query(recentUri, cols, null, null, null);
        if (cursor != null && cursor.moveToPosition(position)) {
            String link = cursor.getString(cursor.getColumnIndex(RecentTable.COLUMN_EPISODE));
            cursor.close();
            return link;
        }
        return null;

    }
 
Example 19
Source File: HistoryFragment.java    From bitcoinpos with MIT License 5 votes vote down vote up
private void getTransactionsAfterClick(int selectedItem) {
    // get DB helper
    mDbHelper = PointOfSaleDb.getInstance(getActivity());

    // Each row in the list stores amount and date of transaction -- retrieves history from DB
    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    // get the following columns:
    String[] tableColumns = { PointOfSaleDb.TRANSACTIONS_COLUMN_CREATED_AT,
            PointOfSaleDb.TRANSACTIONS_COLUMN_TX_ID,
            PointOfSaleDb.TRANSACTIONS_COLUMN_CRYPTOCURRENCY_ADDRESS,
            PointOfSaleDb.TRANSACTIONS_COLUMN_TX_STATUS,
            "_ROWID_",// getting also _ROWID_ to delete the selected tx
            PointOfSaleDb.TRANSACTIONS_COLUMN_CRYPTOCURRENCY,
            PointOfSaleDb.TRANSACTIONS_COLUMN_CRYPTOCURRENCY_AMOUNT    };

    String sortOrder = PointOfSaleDb.TRANSACTIONS_COLUMN_CREATED_AT + " DESC";
    Cursor c = db.query(PointOfSaleDb.TRANSACTIONS_TABLE_NAME, tableColumns, null, null, null, null, sortOrder);
    //moving to position of the cursor according to the selected item to delete the transaction
    if(c.moveToPosition(selectedItem)) {
        String crypto = c.getString(5);
        String status;
        if(c.getInt(3)==0){status=getString(R.string.payment_unconfirmed);}
        else if(c.getInt(3)==1){status=getString(R.string.payment_confirmed);}
        else if(c.getInt(3)==2){status=getString(R.string.payment_pending);}
        else {status=getString(R.string.payment_cancelled);}

        double amount = c.getDouble(6);

        //show dialog
        showInfoDialog(c.getString(2),c.getString(1),status, crypto, amount);
    }

}
 
Example 20
Source File: ConversationView.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
public Cursor getItem (int position)
{
    Cursor c = getCursor();
    c.moveToPosition(position);
    return c;
}