错误CursorWindow有1行,6列无法读取

时间:2018-07-20 01:35:59

标签: android-cursor

我收到此错误:

无法从具有1行6列的CursorWindow中读取第0行,第-1列。 无法从CursorWindow读取行0,col -1。在从游标访问数据之前,请确保游标已正确初始化。

我见过类似的帖子和请求,但没有一个回答我的问题。 你能告诉我我做错了吗?请参阅下面的代码。

       private void displayDatabaseInfo() {
    InventoryDbHelper mDbHelper = new InventoryDbHelper( this );

    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    String[] projection = {

            ProductEntry._ID,
            ProductEntry.COLUMN_PRODUCT_NAME,
            ProductEntry.COLUMN_PRODUCT_PRICE,
            ProductEntry.COLUMN_PRODUCT_QUANTITY,
            ProductEntry.COLUMN_PRODUCT_SUPPLIER_NAME,
            ProductEntry.COLUMN_PRODUCT_SUPPLIER_PHONE,

    };

    Cursor cursor = db.query(
            ProductEntry.TABLE_NAME,
            projection,
            null,
            null,
            null,
            null,
            null,
            null );

    TextView displayView = (TextView) findViewById( R.id.text_view_inventory );


    try {

        // Create a header in the Text View that looks like this:
        // The product table contains <number of rows in Cursor> product.
        // _id - name - price - qtity - supplier - supplier phone
        // In the while loop below, iterate through the rows of the cursor and display
        // the information from each column in this order.

        displayView.setText( "The product table contains " + cursor.getCount() + " products.\n\n" );
        displayView.append( ProductEntry._ID + " - " +
                ProductEntry.COLUMN_PRODUCT_NAME + " - " +
                ProductEntry.COLUMN_PRODUCT_PRICE + " - " +
                ProductEntry.COLUMN_PRODUCT_QUANTITY + " - " +
                ProductEntry.COLUMN_PRODUCT_SUPPLIER_NAME + " - " +
                ProductEntry.COLUMN_PRODUCT_SUPPLIER_PHONE + "\n" );

        // Figure out the index of each column

        int idColumnIndex = cursor.getColumnIndex( ProductEntry._ID );
        int nameColumnIndex = cursor.getColumnIndex( ProductEntry.COLUMN_PRODUCT_NAME );
        int priceColumnIndex = cursor.getColumnIndex( ProductEntry.COLUMN_PRODUCT_PRICE );
        int quantityColumnIndex = cursor.getColumnIndex( ProductEntry.COLUMN_PRODUCT_QUANTITY );
        int supplierNameColumnIndex = cursor.getColumnIndex( ProductEntry.COLUMN_PRODUCT_SUPPLIER_NAME );
        int supplierPhoneColumnIndex = cursor.getColumnIndex( ProductEntry.COLUMN_PRODUCT_SUPPLIER_PHONE );

        // Iterate through all the returned rows in the cursor

        while (cursor.moveToNext()) {

            // Use that index to extract the String or Int value of the word
            // at the current row the cursor is on.

            int currentID = cursor.getInt( idColumnIndex );
            String currentName = cursor.getString( nameColumnIndex );
            int currentPrice = cursor.getInt( priceColumnIndex );
            int currentQuantity = cursor.getInt( quantityColumnIndex );
            String currentSupplierName = cursor.getString( supplierNameColumnIndex );
            int currentSupplierPhone = cursor.getInt( supplierPhoneColumnIndex );
            // Display the values from each column of the current row in the cursor in the TextView
            displayView.append( ("\n" + currentID + " - " +
                    currentName + " - " +
                    currentPrice + " - " +
                    currentQuantity + " - " +
                    currentSupplierName + " - " +
                    currentSupplierPhone ) );
        }


    } finally {
        // Always close the cursor when you're done reading from it. This releases all its
        // resources and makes it invalid.
        cursor.close();
    }
}

1 个答案:

答案 0 :(得分:0)

对于那些将遇到与我相同的问题的人。我自己解决了这个问题。

请参见下面的更新代码,该错误已修复:

同时(cursor.moveToNext()){

        // Use that index to extract the String or Int value of the word
        // at the current row the cursor is on.

        int currentID = cursor.getInt( ColumnIndex 0 );
        String currentName = cursor.getString( ColumnIndex 1 );
        int currentPrice = cursor.getInt( ColumnIndex 2 );
        int currentQuantity = cursor.getInt( ColumnIndex 3 );
        String currentSupplierName = cursor.getString( ColumnIndex 4 );
        int currentSupplierPhone = cursor.getInt( ColumnIndex 5 );