Android检索表数据CursorIndexOutOfBoundsException

时间:2015-06-08 05:14:45

标签: android sqlite cursor indexoutofboundsexception

我需要将所有表数据显示到列表视图中。尝试查看列表视图中的项目时出错。第一个列表视图中有两个项目。单击项目时,需要在下一个列表视图中显示包含该项目的所有项目。数据正在数据库中插入但未在列表视图中显示。我的日志猫也在下面给出..

public class EmployeeDAO {

public static final String TAG = "EmployeeDAO";

private Context mContext;

// Database fields
private SQLiteDatabase mDatabase;
private DBHelper mDbHelper;
private String[] mAllColumns = { DBHelper.COLUMN_LATLONG_ID, DBHelper.COLUMN_LOCATION_NAME,
        DBHelper.COLUMN_LOCATION_ADDRESS,DBHelper.COLUMN_LATITUDE,DBHelper.COLUMN_LONGITUDE};

public EmployeeDAO(Context context) {
    mDbHelper = new DBHelper(context);
    this.mContext = context;
    // open the database
    try {
        open();
    }
    catch(SQLException e) {
        Log.e(TAG, "SQLException on openning database " + e.getMessage());
        e.printStackTrace();
    }
}

public void open() throws SQLException {
    mDatabase = mDbHelper.getWritableDatabase();
}

public void close() {
    mDbHelper.close();
}

public Employee createEmploye(String place, String location, String latitude, String longitude, long companyId) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.COLUMN_LOCATION_NAME, place);
    values.put(DBHelper.COLUMN_LOCATION_ADDRESS, location);
    values.put(DBHelper.COLUMN_LATITUDE, latitude);
    values.put(DBHelper.COLUMN_LONGITUDE, longitude);
    values.put(DBHelper.COLUMN_LATLONG_LOC_ID, companyId);
    long insertId = mDatabase.insert(DBHelper.TABLE_LATLONG, null, values);
    Cursor cursor = mDatabase.query(DBHelper.TABLE_LATLONG,
            mAllColumns, DBHelper.COLUMN_LATLONG_ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    Employee newEmployee = cursorToEmploye(cursor);
    cursor.close();
    return newEmployee;
}

public void deleteEmployee(Employee employee) {
    long id = employee.getId();
    System.out.println("the deleted employee has the id: " + id);
    mDatabase.delete(DBHelper.TABLE_LATLONG, DBHelper.COLUMN_LATLONG_ID + " = " + id, null);
}

public List<Employee> getAllEmployees() {
    List<Employee> listEmployees = new ArrayList<Employee>();

    Cursor cursor = mDatabase.query(DBHelper.TABLE_LATLONG,
            mAllColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Employee employee = cursorToEmploye(cursor);
        listEmployees.add(employee);
        cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return listEmployees;
}

public List<Employee> getEmployeesOfCompany(long companyId) {         <-- getting error code here
    List<Employee> listEmployees = new ArrayList<Employee>();

    Cursor cursor = mDatabase.query(DBHelper.TABLE_LATLONG, mAllColumns
            , DBHelper.COLUMN_LATLONG_LOC_ID + " = ?",
            new String[] { String.valueOf(companyId) }, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Employee employee = cursorToEmploye(cursor);
        listEmployees.add(employee);
        cursor.moveToNext();
    }

    cursor.close();
    return listEmployees;
}

private Employee cursorToEmploye(Cursor cursor) {   <--- getting error code here
    Employee employee = new Employee();
    employee.setId(cursor.getLong(0));
    employee.setFirstName(cursor.getString(1));
    employee.setLastName(cursor.getString(2));


    long companyId = cursor.getLong(3);
    CompanyDAO dao = new CompanyDAO(mContext);
    Locations company = dao.getCompanyById(companyId);
    if(company != null)
        employee.setCompany(company);

    return employee;
}

}

logcat的

06-10 01:34:15.175: E/AndroidRuntime(16289): FATAL EXCEPTION: main
06-10 01:34:15.175: E/AndroidRuntime(16289): Process: com.example.locationstable, PID: 16289
06-10 01:34:15.175: E/AndroidRuntime(16289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locationstable/com.example.locationstable.ListEmployeesActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.os.Looper.loop(Looper.java:136)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at java.lang.reflect.Method.invoke(Method.java:515)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at dalvik.system.NativeStart.main(Native Method)
06-10 01:34:15.175: E/AndroidRuntime(16289): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.CompanyDAO.cursorToLocation(CompanyDAO.java:111)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.CompanyDAO.getCompanyById(CompanyDAO.java:105)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.EmployeeDAO.cursorToEmploye(EmployeeDAO.java:113)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.EmployeeDAO.getEmployeesOfCompany(EmployeeDAO.java:94)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.ListEmployeesActivity.onCreate(ListEmployeesActivity.java:85)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.Activity.performCreate(Activity.java:5231)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-10 01:34:15.175: E/AndroidRuntime(16289):    ... 11 more

1 个答案:

答案 0 :(得分:0)

您没有在某个方法中关闭光标

public Locations getCompanyById(long id) {
Cursor cursor = mDatabase.query(SQLiteDatabaseData.TABLE_LOC, mAllColumns,
        SQLiteDatabaseData.COLUMN_ID + " = ?",
        new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null) {
    cursor.moveToFirst();
}

Locations locations = cursorToLocation(cursor);
// Close the cursor
cursor.close();
return locations;
}