CursorOutofBoundsException索引-1请求大小为0

时间:2017-07-19 07:50:32

标签: android android-sqlite android-cursor android-database

我有一个名为updateparty的数据库,我必须从中创建一个类型为hashmap string的数组列表。当我尝试从db中检索数据时,光标不会移动到下一条记录。

以下是db插入的代码。

for (int i = 0; i < poslist.size(); i++) {


                SQLiteDatabase db7 = db.getWritableDatabase();
                ContentValues values5= new ContentValues();
                values5.put(DBManager.TableInfo.KEYID, ID1);
                values5.put(DBManager.TableInfo.DOCU, document);
                values5.put(DBManager.TableInfo.ATTEND,attendancelist.get(i));
                values5.put(DBManager.TableInfo.EMAIL, emaillist.get(i));
                values5.put(DBManager.TableInfo.PARTY,partytypelist.get(i) );
                values5.put(DBManager.TableInfo.BIO,biometriclist.get(i));
                values5.put(DBManager.TableInfo.KEY_LOGIN_USER,username2);
                String condition5 = DBManager.TableInfo.DOCU + " =?";
                Cursor cursor5 = db7.query(DBManager.TableInfo.UPDATEPARTY, null, condition5, new String[]{DBManager.TableInfo.ATTEND}, null, null, null);
                long status5 = db7.insert(DBManager.TableInfo.UPDATEPARTY, null, values5);
                System.out.println( "Parties insert : " + status5);
                cursor5.close();
                db7.close();
            }

从db上面创建arraylist的代码:

public ArrayList<HashMap<String, String>>getPartypost(DBOperation db) {
    ArrayList<HashMap<String, String>> listParties11 = new ArrayList<HashMap<String, String>>();

    try {

        String query = "select * from " + DBManager.TableInfo.UPDATEPARTY + " where " + DBManager.TableInfo.KEY_LOGIN_USER + " = '" + GenericMethods.email + "'";
        System.out.println("query:"+query);
        SQLiteDatabase sqlite1 = db.getWritableDatabase();
        Cursor c = sqlite1.rawQuery(query, null);

        while(c.moveToNext()) {
                    HashMap<String, String> selectiondetails10 = new HashMap<String, String>();
           System.out.println("value:"+c.getString(c.getColumnIndex(DBManager.TableInfo.DOCU)));
                    selectiondetails10.put("document_id", c.getString(c.getColumnIndex(DBManager.TableInfo.DOCU)));
                    selectiondetails10.put("att_id", c.getString(c.getColumnIndex(DBManager.TableInfo.ATTEND)));
                    selectiondetails10.put("email", c.getString(c.getColumnIndex(DBManager.TableInfo.EMAIL)));
                    selectiondetails10.put("party_type", c.getString(c.getColumnIndex(DBManager.TableInfo.PARTY)));
                    selectiondetails10.put("biometric", c.getString(c.getColumnIndex(DBManager.TableInfo.BIO)));
                    selectiondetails10.put("exec_email", c.getString(c.getColumnIndex(DBManager.TableInfo.KEY_LOGIN_USER)));

                    listParties11.add(selectiondetails10);

                }

                closeCursor(c);
            //}
       // }
        System.out.println("list size:"+listParties11.size());
        } catch (Exception e) {
        // TODO: handle exception.
        e.printStackTrace();
    }
    db.close();
    return listParties11;
}

这是错误消息:

enter image description here

我需要的是从这个db

创建一个arraylist

2 个答案:

答案 0 :(得分:0)

将您的插入代码更改为:

            SQLiteDatabase db7 = db.getWritableDatabase();
            ContentValues values5= new ContentValues();
            for (int i = 0; i < poslist.size(); i++) {
            values5.put(DBManager.TableInfo.KEYID, ID1);
            values5.put(DBManager.TableInfo.DOCU, document);
            values5.put(DBManager.TableInfo.ATTEND,attendancelist.get(i));
            values5.put(DBManager.TableInfo.EMAIL, emaillist.get(i));
            values5.put(DBManager.TableInfo.PARTY,partytypelist.get(i) );
            values5.put(DBManager.TableInfo.BIO,biometriclist.get(i));
            values5.put(DBManager.TableInfo.KEY_LOGIN_USER,username2);
            String condition5 = DBManager.TableInfo.DOCU + " =?";
            Cursor cursor5 = db7.query(DBManager.TableInfo.UPDATEPARTY, null, condition5, new String[]{DBManager.TableInfo.ATTEND}, null, null, null);
            long status5 = db7.insert(DBManager.TableInfo.UPDATEPARTY, null, values5);
            System.out.println( "Parties insert : " + status5);
            cursor5.close();
            db7.close();
        }

答案 1 :(得分:0)

使用以下代码,while循环应如下所示: 的而(!c.isAfterLast())

public ArrayList<HashMap<String, String>>getPartypost(DBOperation db) {
    ArrayList<HashMap<String, String>> listParties11 = new ArrayList<HashMap<String, String>>();

    try {

        String query = "select * from " + DBManager.TableInfo.UPDATEPARTY + " where " + DBManager.TableInfo.KEY_LOGIN_USER + " = '" + GenericMethods.email + "'";
        System.out.println("query:"+query);
        SQLiteDatabase sqlite1 = db.getWritableDatabase();
        Cursor c = sqlite1.rawQuery(query, null);
             c.moveToFirst();
        while(!c.isAfterLast()) {
                    HashMap<String, String> selectiondetails10 = new HashMap<String, String>();
           System.out.println("value:"+c.getString(c.getColumnIndex(DBManager.TableInfo.DOCU)));
                    selectiondetails10.put("document_id", c.getString(c.getColumnIndex(DBManager.TableInfo.DOCU)));
                    selectiondetails10.put("att_id", c.getString(c.getColumnIndex(DBManager.TableInfo.ATTEND)));
                    selectiondetails10.put("email", c.getString(c.getColumnIndex(DBManager.TableInfo.EMAIL)));
                    selectiondetails10.put("party_type", c.getString(c.getColumnIndex(DBManager.TableInfo.PARTY)));
                    selectiondetails10.put("biometric", c.getString(c.getColumnIndex(DBManager.TableInfo.BIO)));
                    selectiondetails10.put("exec_email", c.getString(c.getColumnIndex(DBManager.TableInfo.KEY_LOGIN_USER)));

                    listParties11.add(selectiondetails10);
c.moveToNext();
                }

                closeCursor(c);
            //}
       // }
        System.out.println("list size:"+listParties11.size());
        } catch (Exception e) {
        // TODO: handle exception.
        e.printStackTrace();
    }
    db.close();
    return listParties11;
}