Android Cursor从第0行开始

时间:2015-03-11 15:06:38

标签: java android cursor

我有一个程序,我在其中创建一个表:

 //CAR_TABLE COLUMNS
    public static final String CAR_COLUMN_ID = "ID";
    public static final String CAR_COLUMN_ID_BARCODE = "ID_BARCODE";
    public static final String CAR_COLUMN_Hersteller ="Hersteller";
    public static final String CAR_COLUMN_KFZTyp = "KFZTyp";
    public static final String CAR_COLUMN_Farbe = "Farbe";
    public static final String CAR_COLUMN_AbteilungID = "AbteilungID";
    public static final String CAR_COLUMN_SONDERAUSSTATTUNG ="Sonderausstattung";
    public static final String CAR_COLUMN_Ausgeliefert = "Ausgeliefert";
    public static final String CAR_COLUMN_ProgrammID ="ProgrammID";
            public void onCreate(SQLiteDatabase db) {
                String CREATE_CAR_TABLE = "CREATE TABLE " + TABLE_CARS + "(\n" +
                        CAR_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \n" +
                        CAR_COLUMN_ID_BARCODE + " TEXT, \n" +
                        CAR_COLUMN_Hersteller + " TEXT, \n" +
                        CAR_COLUMN_KFZTyp + " TEXT, \n" +
                        CAR_COLUMN_Farbe + " TEXT, \n" +
                        CAR_COLUMN_AbteilungID + " INTEGER, \n" +
                        CAR_COLUMN_Ausgeliefert + " TEXT, \n" +
                        CAR_COLUMN_SONDERAUSSTATTUNG + " TEXT, \n" +
                        CAR_COLUMN_ProgrammID + " INTEGER \n" +
                        ");";

数据库创建正常,我可以正常添加条目。

问题在于,当我想从数据库中检索一行时,它会告诉我以下内容

03-11 16:00:00.470: E/CursorWindow(7465): Failed to read row 0, column 8 from a CursorWindow which has 1 rows, 8 columns.
03-11 16:00:00.470: D/AndroidRuntime(7465): Shutting down VM
03-11 16:00:00.470: E/AndroidRuntime(7465): FATAL EXCEPTION: main
03-11 16:00:00.470: E/AndroidRuntime(7465): Process: com.example.prog3, PID: 7465
03-11 16:00:00.470: E/AndroidRuntime(7465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prog3/com.example.prog3.ShowCars}: java.lang.IllegalStateException: Couldn't read row 0, col 8 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

我的getAllCars方法:

public List<AutoEntry> getAllCars() {
    List<AutoEntry> autoentrys = new ArrayList<AutoEntry>();
    openDatabase();
    Cursor cursor = database.query(TABLE_CARS, allColumnsAuto, null, null, null, null, null);

    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        AutoEntry autoentry = cursorToCar(cursor);
        autoentrys.add(autoentry);
        cursor.moveToNext();
    }
    cursor.close();
    closeDatabase();
    return autoentrys;
}

这是我的Cursor方法:

private AutoEntry cursorToCar(Cursor cursor) {
    AutoEntry autoentry = new AutoEntry();
    autoentry.setFarbe(cursor.getString(1));
    autoentry.setHersteller(cursor.getString(2));
    autoentry.setName(cursor.getString(3));
    autoentry.setSonderaustattung(cursor.getString(4));
    autoentry.setAbteilungID(cursor.getLong(5));
    autoentry.setProgrammID(cursor.getLong(6));
    autoentry.setAusgeliefert(cursor.getString(7));
    autoentry.setBarcode(cursor.getInt(8));
    return autoentry;
  }

对于completness,这是我的Insert方法:

public void insertCar(String hersteller, String name, String farbe, String sonderausstattung, int abteilungID, String ausgeliefert, int barcode, int programmID) {

        ContentValues values = new ContentValues();

        values.put(Databasehandler.CAR_COLUMN_Hersteller, hersteller);
        values.put(Databasehandler.CAR_COLUMN_KFZTyp, name);
        values.put(Databasehandler.CAR_COLUMN_Farbe, farbe);
        values.put(Databasehandler.CAR_COLUMN_SONDERAUSSTATTUNG, sonderausstattung);
        values.put(Databasehandler.CAR_COLUMN_Ausgeliefert, ausgeliefert);
        values.put(Databasehandler.CAR_COLUMN_AbteilungID, abteilungID);
        values.put(Databasehandler.CAR_COLUMN_ID_BARCODE, barcode);
        values.put(Databasehandler.CAR_COLUMN_ProgrammID, programmID);

        try {
            openDatabase();
            long insertID = database.insert(this.TABLE_CARS, null, values);

            Cursor cursor = database.query(this.TABLE_CARS, allColumnsAuto, Databasehandler.CAR_COLUMN_ID + " = " + insertID, null, null, null, null);
            cursor.moveToFirst();
            AutoEntry autoentry = cursorToCar(cursor);
            cursor.close();
            closeDatabase();
        } catch (Exception ex) {
            System.out.println("Error  inserting entry!" + ex.toString());

        }
}

我应该如何解决这个问题,他从第1行开始,或者我的数据存储在第0行?

修改

private String[] allColumnsAuto = { Databasehandler.CAR_COLUMN_ID,
        Databasehandler.CAR_COLUMN_Farbe,
        Databasehandler.CAR_COLUMN_ID_BARCODE,
        Databasehandler.CAR_COLUMN_Hersteller,
        Databasehandler.CAR_COLUMN_KFZTyp,
        Databasehandler.CAR_COLUMN_SONDERAUSSTATTUNG,
        Databasehandler.CAR_COLUMN_AbteilungID,
        Databasehandler.CAR_COLUMN_ProgrammID,
        Databasehandler.CAR_COLUMN_Ausgeliefert };

1 个答案:

答案 0 :(得分:3)

由于列索引基于0 (因为行是 - 以及Java中的所有内容),因此您无法引用非现有列8

所以简单地替换这个

private AutoEntry cursorToCar(Cursor cursor) {
    AutoEntry autoentry = new AutoEntry();
    autoentry.setFarbe(cursor.getString(1));
    autoentry.setHersteller(cursor.getString(2));
    autoentry.setName(cursor.getString(3));
    autoentry.setSonderaustattung(cursor.getString(4));
    autoentry.setAbteilungID(cursor.getLong(5));
    autoentry.setProgrammID(cursor.getLong(6));
    autoentry.setAusgeliefert(cursor.getString(7));
    autoentry.setBarcode(cursor.getInt(8));
    return autoentry;
  }

用这个

private AutoEntry cursorToCar(Cursor cursor) {
    AutoEntry autoentry = new AutoEntry();
    autoentry.setFarbe(cursor.getString(0));
    autoentry.setHersteller(cursor.getString(1));
    autoentry.setName(cursor.getString(2));
    autoentry.setSonderaustattung(cursor.getString(3));
    autoentry.setAbteilungID(cursor.getLong(4));
    autoentry.setProgrammID(cursor.getLong(5));
    autoentry.setAusgeliefert(cursor.getString(6));
    autoentry.setBarcode(cursor.getInt(7));
    return autoentry;
  }

如果您按列名称引用列,而不是列索引

,则会更好

类似的东西:

cursor.getString(cursor.getColumnIndex("Column_Name"));

在你的情况下:

autoentry.setHersteller(cursor.getString(cursor.getColumnIndex(CAR_COLUMN_Hersteller)));