开发者

CursorIndexOutOfBoundException: Index 1 requested, with a size of 1

it's me once more, having problems with sqlite for Android

I currently get a "CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1" However, I had this exception with Index -1 then inserted a cursor.moveToFirst(), then I had this with index 0 , then开发者_运维百科 did cursor.moveToNext(); What I wanna do with my code? I want the information of a selected item (that's why the selectionArgs are there with sQueryid. What am I doing wrong?

    Cursor c = a.managedQuery(uri, 
            projection, //projection
            "_ID=?", //selection string
            new String[]{sQueryid}, //selection args array of strings
            DepotTableMetaData.DEFAULT_SORT_ORDER); //sort order
    c.moveToFirst();
    c.moveToNext();
    int iqrcode = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_QRCODE);     
    int iname = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_NAME);
    int iamount = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_AMOUNT);
    int iunit = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_UNIT);
    int ippu = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_PPU);
    int itotal = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_TOTAL);
    int icomment = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_COMMENT);


        //Gather values
        String id = c.getString(Integer.parseInt(queryid.toString()));

        String name = c.getString(iname);
        String amount = c.getString(iamount);
        String unit = c.getString(iunit);
        String ppu = c.getString(ippu);
        String total = c.getString(itotal);
        String comment = c.getString(icomment);
        String qrcode = c.getString(iqrcode);

        String[] info = new String[]{id,name,amount,unit,ppu,total,comment,qrcode};


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);
}


I stopped getting this problem after I started coding it like this

if (c != null ) {
    Cursor c = sql.rawQuery("SELECT...");
    c.moveToFirst();
    for (int i = 0; i < c.getCount(); i++) {
        //do something
    c.moveToNext();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜