CursorIndexOutOfBoundException: Index 1 requested, with a size of 1 CursorIndexOutOfBoundException: Index 1 requested, with a size of 1 sqlite sqlite

CursorIndexOutOfBoundException: Index 1 requested, with a size of 1


moveToFirst takes you to the first (and only) result. The next call to moveToNext takes you to the second result (index 1) which doesn't exist.

Try removing the c.moveToNext();


Move the cursor.moveToFirst(); and there you are. Since moveToFirst() takes the cursor pointer to the first record and thats what you need. Do not do moveToNext() after you did moveToFirst() as you did in the snippet.


You need to include more information in your post; the code shows variables that are being used but we have no idea what they are (uri, project, sQueryid, etc.). You have an sQueryid String and also are trying to send the String value of a variable called queryid to the parseInt method--are these the same variables, with a typo? If not, you should try alternate naming so it's less confusing.

We also can't see what method thsi code is in; you mention you're getting issues when clicking on your ListView, but is this where the code resides? If so, calling moveToFirst() and/or moveToNext() on your Cursor is not what you want, and you wouldn't be getting the cursor by caling managedQuery there. Instead, you'd want to use the position information passed in to the method and the ListView's adapter. With a little more of your code, I could give an example.

If you're just trying to cycle through all of the rows in your Cursor query, you probably want to do something like this:

while (c.moveToNext()) {    String name = c.getString(c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_NAME));    String amount = c.getString(c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_AMOUNT));    // ...    // do something with the data for this row}

Edit (per the code listed in your comment):

I think your issue is with the id that you're passing to the ShowActivity guy. In your ShowActivity class, make a class-level variable for your Cursor you're using on the SimpleCursorAdapter for the ListView so that you can access it in other methods. So, something like:

public class ShowActivity extends ListActivity implements OnItemClickListener {    Cursor mCursor;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // ...        mCursor = managedQuery(ContentProviderMetaData.DepotTableMetaData.CONTENT_URI, projection, null, null, ContentProviderMetaData.DepotTableMetaData.DEFAULT_SORT_ORDER);        // ...        SimpleCursorAdapter simpleadapter = new SimpleCursorAdapter(this,R.layout.list_entry, mCursor, columns, to);        // ...

Then, your onItemClick method should look something like this:

public void onItemClick(AdapterView<?> adapterView, View target, int position, long id) {    Log.d("ShowActivity", "clicked @ " + position);    mCursor.moveToPosition(position);    final String itemId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));    Intent openAdvanced = new Intent(ShowActivity.this,AdvancedViewActivity.class);    openAdvanced.putExtra("ID", itemId);    startActivity(openAdvanced);}