ERROR: CursorIndexOutOfBoundsException [closed]

0
public boolean insertData(int id_pedido, int id_item, String nome, int quantidade) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1_ITEM_ORDER, id_pedido);
    contentValues.put(COL_2_ITEM_ORDER, id_item);
    contentValues.put(COL_3_ITEM_ORDER, nome);
    contentValues.put(COL_4_ITEM_ORDER, quantidade);
    String sql = "SELECT preco FROM " + TABLE_PRECO + " WHERE id_item = " + id_item;
    Cursor cursor = db.rawQuery(sql,null);
    cursor.moveToFirst();
    String preco_unitario = cursor.getString(cursor.getColumnIndex(COL_3_PRICES));
    cursor.close();
    double total = quantidade * Long.parseLong(preco_unitario);
    contentValues.put(COL_5_ITEM_ORDER, preco_unitario);
    contentValues.put(COL_6_ITEM_ORDER, total);
    long result = db.insert(TABLE_ITEM_PEDIDO, null, contentValues);
    if (result == -1) {
        return false;
    } else {
        return true;
    }
  

ERROR: android.database.CursorIndexOutOfBoundsException: Index 0   requested, with a size of 0

Can anyone help me please? I've made a lot of changes and nothing.

    
asked by anonymous 03.08.2016 / 20:24

1 answer

1

Speak Gabriel,

This is probably because your table is still empty, so try:

if(cursor.getCount() > 0){
  String preco_unitario = cursor.getString(cursor.getColumnIndex(COL_3_PRICES));
}

Hugs.

    
03.08.2016 / 20:28