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 7851869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:16:59+00:00 2026-06-02T19:16:59+00:00

I have a selection of sqlite entries displayed in a ListView, and I have

  • 0

I have a selection of sqlite entries displayed in a ListView, and I have their primary keys in a parallel array. When a user selects an item, the item is sent to an Editor activity (so the user can edit that entry). The way I programmed it, the Editor activity class retrieves the entry from the database using its row number (meaning position), using the Cursor’s moveToPosition function.

How can I use an entry’s primary key to find out its row number?

EDIT :
I have found a solution, wherein I first retrieve a selection of entries from the database, and then go back and retrieve the primary keys of those entries. I store the entries in one array, and the primary keys in another array.

Hopefully you’ll be able to give clearer responses after seeing the code. I’m looking for a more elegant solution, although this seems to work.

Here is the relevant code.

CalendarMenu.java

@Override
public void onCreate(Bundle savedInstance){
  incomeManager = new SQLManagerIncome(this);
incomeManager.open();

  //This method (see below) gets all of the SQLite entries between two dates
    incomeArray = incomeManager.getDatedEntriesString(
            currentDay.getTimeInMillis(), 
              nextDay.getTimeInMillis());

  //This method (see below) gets the primary keys of all the entries retrieved 
  //in the previous method

    incomeArrayKeys = incomeManager.getDatedKeys(
           currentDay.getTimeInMillis(),
            nextDay.getTimeInMillis());
    incomeManager.close();

    // Sending the SQL info to the ListViews
    incomeAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, incomeArray);
    dayList.setAdapter(incomeAdapter);
}


    @Override
public void onItemClick(AdapterView<?> listView, View view, int pos, long id) {
    Bundle basket = new Bundle();
    // we need to send the position of an entry inside the entire database to the
    // Editor class. So we'll use the primary key of an entry to retrieve its position.

    switch (listView.getId()) {
    case R.id.dayList:
        int entryPos = 0;
        //incomeArrayKeys holds all of the primary keys. It is a parallel
        //array to the array that is displayed in the listview 
        //(using a standard ArrayAdapter)
            incomeManager.open();
            entryPos = incomeManager.findPosByKey(incomeArrayKeys[pos]);
            incomeManager.close();

        basket.putLong("index", entryPos);
                    Class dayClass;
            try {
                dayClass = Class.forName("com.shulim.maaser.EditIncome");
                Intent dayIntent = new Intent(CalendarMenu.this, dayClass);
                dayIntent.putExtras(basket);
                startActivityForResult(dayIntent, 0);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        break;
}

SQLManagerIncome.java

public String[] getDatedEntriesString(Long firstDay, Long lastDay){
    String[] columns = new String[] { KEY_ROWID, KEY_DESCRIPTION,
            KEY_AMOUNT, KEY_DATE, KEY_OWED };
    String selection = KEY_DATE + " >=" + firstDay + " AND " + KEY_DATE
            + "<" + lastDay;
    Cursor c = ourDatabase.query(INCOME_TABLE, columns, selection, null, null, null, null);
    int iDescription = c.getColumnIndex(KEY_DESCRIPTION);
    int iAmount = c.getColumnIndex(KEY_AMOUNT);
    ArrayList<String> keys = new ArrayList<String>();

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        Long dateMillis = c.getLong(3);
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(dateMillis);
        int mYear = cal.get(Calendar.YEAR);
        int mMonth = cal.get(Calendar.MONTH)+1;
        int mDay = cal.get(Calendar.DAY_OF_MONTH);
        String date = mMonth + "-" + mDay + "-" + mYear;
        String result = "Desc: " + c.getString(iDescription) 
                + ". Amount: $"
                + c.getString(iAmount) + ". Date: "
                + date + "\n";
        keys.add(result);
    }
    String[] stringDateKeys = new String[keys.size()];
    stringDateKeys = keys.toArray(stringDateKeys);
    c.close();
    return stringDateKeys;
}

public Integer[] getDatedKeys(Long firstDay, Long lastDay) {
    String[] columns = new String[] { KEY_ROWID, KEY_DESCRIPTION,
             KEY_AMOUNT, KEY_DATE };
    String selection = KEY_DATE + " >=" + firstDay + " AND " + KEY_DATE
            + "<" + lastDay;
    Cursor c = ourDatabase.query(INCOME_TABLE, columns, selection, null,
            null, null, null);
    int iRow = c.getColumnIndex(KEY_ROWID);
    ArrayList<Integer> keys = new ArrayList<Integer>();

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        int result = c.getInt(iRow);
        keys.add(result);
    }

        Integer[] intKeys = new Integer[keys.size()];
        intKeys = keys.toArray(intKeys);
        c.close();
        return intKeys;
}

public int findPosByKey(Integer integer) {
    String[] columns = new String[] { KEY_ROWID, KEY_DESCRIPTION,
            KEY_AMOUNT, KEY_DATE };
    Cursor c = ourDatabase.query(INCOME_TABLE, columns, null, null,
            null, null, null);
    int iRow = c.getColumnIndex(KEY_ROWID);
    int position = 0;
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        if (c.getInt(iRow) == integer) {
            break;
        }
        position++;
    }
    c.close();
    return position;
}   
  • 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-06-02T19:17:00+00:00Added an answer on June 2, 2026 at 7:17 pm

    The way I programmed it, the Editor activity class retrieves the entry from the database using its row number (meaning position), using the Cursor’s moveToPosition function.

    You don’t need to use the cursor’s moveToPosition function. Instead, there’s a much better solution: use the primary key that you already have available. The primary key allows a very simple and efficient SQL query that will return only the exact row you want. I don’t know your column names, but an example SQL query to load this row would be

    SELECT foo, bar, baz
    FROM FooBar
    WHERE pk = userSelectedPrimaryKey;
    

    where userSelectedPrimaryKey is inserted into the statement via a query builder.

    Of course this is just a rough sketch, but the point is that this query will return exactly one row, which is the only row with the primary key value that the user selected. This is simpler, and improves performance because there is no need to iterate through unnecessary rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Spatial Selection implemented on Map in my Web Application, which selects the
I have a listview that is sourced by an sqlite db. I call fillData()
i have this selection query to fetch rows from the sqlite database of android
I have a selection of products within my wordpress site. They are configurable products
I have a selection for some items using a spinner widget. At the moment
I have a selection of elements in a jQuery object. I want to animate
I have a selection of a huge set of data and I would like
I have a selection of elements that I'm trying to filter down based on
So I have a selection list, and if the default value hasn't been changed
I have added checkbox selection model to grid panel. It works fine in IE8

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.