错误:CursorIndexOutOfBoundsException:索引0要求,大小为0 [英] ERROR : CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

查看:1021
本文介绍了错误:CursorIndexOutOfBoundsException:索引0要求,大小为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海兰伙计们,我是初学者Android开发者。我需要你的帮助。我想数据插入到sqlite的tblorder和orderdtl的2表。在orderdtl我必须从列表视图中插入多个项目的数据。我尝试敬酒,我要插入的所有变量。他们所有的出现。但是当我尝试将其保存。我得到这些错误。
这是我的DBDataSource.java

 公共秩序createorder(订单ID字符串,字符串笔记,长outletid){
    ContentValues​​值=新ContentValues​​();
    values​​.put(DBHelper.ORDER_ID,订单id); //插入一个字符串
    values​​.put(DBHelper.NOTES,备注); //插入一个int
    values​​.put(DBHelper.OUTLET_ID,outletid); //插入一个int    长insertId = database.insert(DBHelper.TABLE_ORDER,空,
            值);
    光标光标= database.query(DBHelper.TABLE_ORDER,
            allorder,DBHelper.ORDER_ID +=+ insertId,空,
            NULL,NULL,NULL);        cursor.moveToFirst();
        订购neworder = cursorToorder(光标);
        cursor.close();
        返回neworder;}
 私立秩序cursorToorder(光标光标){
    为了令=新订单();
    order.setorderid(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));
    order.setorderdate(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_DATE)));
    order.setnotes(cursor.getString(cursor.getColumnIndex(DBHelper.NOTES)));
    order.setoutletid(cursor.getLong(cursor.getColumnIndex(DBHelper.OUTLET_ID)));
    返回秩序;
    }

错误请参阅本code

 光标光标= database.query(DBHelper.TABLE_ORDER,
            allorder,DBHelper.ORDER_ID +=+ insertId,空,
            NULL,NULL,NULL);

这code

  order.setorderid(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));

订单ID是字符串,我尝试从yyyyMMddHHmmss.this得到的是code:

 私人字符串的OrderID(){
    SimpleDateFormat的日期格式=新的SimpleDateFormat(
            YYYYMMDDHHMMSS,Locale.getDefault());
    日期日期=新的日期();
    返回dateFormat.format(日期);

}
我会很感激的任何帮助,你give.Thank你。<​​/ P>

解决方案

查询不匹配任何行。检查的结果 moveToFirst()来看看操作是否成功,然后才访问游标数据。

例如:

 为了neworder = NULL;
如果(cursor.moveToFirst()){
    订购neworder = cursorToorder(光标);
}
cursor.close();
返回neworder;

insertId 您从获得插入()是该行的sqlite的行ID。很可能不一样, ORDER_ID 。为了使一列ROWID的别名,其声明为 INTEGER PRIMARY KEY

Hy Guys, I am Beginner Android Developer. I need your help. i want to insert data into 2 tables of sqlite tblorder, and orderdtl. on orderdtl i have to insert data from multiple item from listview. i try to toast all variable that i want to inserted. their all appears. but when i try to save it. i get those error. this is my DBDataSource.java

public order createorder(String orderid, String notes, long outletid) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.ORDER_ID, orderid); // inserting a string
    values.put(DBHelper.NOTES, notes); // inserting an int
    values.put(DBHelper.OUTLET_ID, outletid); // inserting an int

    long insertId = database.insert(DBHelper.TABLE_ORDER, null,
            values);
    Cursor cursor = database.query(DBHelper.TABLE_ORDER,
            allorder, DBHelper.ORDER_ID + " = " + insertId, null,
            null, null, null);

        cursor.moveToFirst();
        order neworder = cursorToorder(cursor);
        cursor.close();
        return neworder;}
 private order cursorToorder(Cursor cursor) {
    order order = new order();
    order.setorderid(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));
    order.setorderdate(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_DATE)));
    order.setnotes(cursor.getString(cursor.getColumnIndex(DBHelper.NOTES)));
    order.setoutletid(cursor.getLong(cursor.getColumnIndex(DBHelper.OUTLET_ID)));
    return order;
    }

The error refer to this code

Cursor cursor = database.query(DBHelper.TABLE_ORDER,
            allorder, DBHelper.ORDER_ID + " = " + insertId, null,
            null, null, null);

And this code

order.setorderid(cursor.getString(cursor.getColumnIndex(DBHelper.ORDER_ID)));

orderid is string, i try to get from yyyyMMddHHmmss.this is the code:

    private String orderid(){
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyyMMddHHmmss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);

} I would be very grateful for any help that you give.Thank You.

解决方案

The query didn't match any rows. Check the result of moveToFirst() to see whether the operation succeeded and only then access cursor data.

Example:

order neworder = null;
if (cursor.moveToFirst()) {
    order neworder = cursorToorder(cursor);
}
cursor.close();
return neworder;

The insertId you get from insert() is the sqlite row id for the row. It's likely not the same as ORDER_ID. To make a column an alias for rowid, declare it as INTEGER PRIMARY KEY.

这篇关于错误:CursorIndexOutOfBoundsException:索引0要求,大小为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆