Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6127879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:33:47+00:00 2026-05-23T16:33:47+00:00

I am trying to create a single-choice (e.g., radio button) list of items that

  • 0

I am trying to create a single-choice (e.g., radio button) list of items that are queried from a database. To work with the currently selected row, I need to use the getCheckedItemPosition() and setItemChecked() methods of ListView.

I have a ListView that has a SimpleCursorAdapter set on it. When I retrieve the currently selected item, I have its row ID from the database, which I need to use to find the appropriate item and manually set it to be selected via the aforementioned methods. In other words, I need to map the status of a row ID to a necessarily monotonic row ID (because the setItemChecked() method accepts a position ID, not a database row ID, and in my database I can remove items).

So is there a way I can get a position id from a table row id? I’d rather not resort to doing a search if possible.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:33:48+00:00Added an answer on May 23, 2026 at 4:33 pm

    I ended up just creating a simple method that searches through all the items (which are assumed to be in order):

    /**
     * Since Android apparently provides no way to do this, do a simple binary
     * search for the item position based on its row id.
     * @param adapter    The adapter to use
     * @param row    The rowId to search for
     * @param left
     * @param right
     * @return    Position, or -1 if not found
     */
    public int getPositionFromRowId(ListAdapter adapter, long row, int left, int right) {
        if (left > right)
            return -1;
        int middle = (left + right) / 2;
        if (adapter.getItemId(middle) == row)
            return middle;
        if (adapter.getItemId(middle) > row)
            return getPositionFromRowId(adapter, row, left, middle - 1);
        else
            return getPositionFromRowId(adapter, row, middle + 1, right);
    }
    

    Edit: Use this code by doing something like this:

    getPositionFromRowId(myListAdapter, row, 0, myListAdapter.getCount());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an Alert Dialog with a list of single-choice items
I am trying to create buttons that play single sound files and one button
I am trying to create a single query that will pull results from a
I am trying to create a program that reads multiple choice answers from a
I'm trying to create a single, one-off page that will combine information from several
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to create a single array that contains all of the values
I'm trying to create a single page form to create a 'work item'. One
I'm trying to create a single multiple choice question. In the example below, I
I am trying to display sample alert dialog with single choice items and facing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.