Sqlite 游标不返回数据



光标携带数据出现问题,调试时,我得到了"java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed."

但我仍然无法调整光标问题,我知道在光标返回数据之前不应该关闭连接,但我不知道如何调整。

我想从getEmpData方法中获取一些信息,然后传递它。

我的方法:

public Cursor getEmpData(Integer employeeID)
{
EmpDept = getReadableDatabase();
Integer[] empRow = {employeeID};
Cursor c = EmpDept.rawQuery("Select name, Title, phone, email from Employee where EmpID like ?", new String[]{employeeID.toString()});
if (c != null)
{
c.moveToFirst();
}
EmpDept.close();
return c;
}

列表视图部分:

单击名称时,应将数据移动到新的活动中。

namelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String name = namelist.getItemAtPosition(position).toString();
//Getting ID of emp,dept
Cursor empID = Emp.getEmpID(name);
Cursor DepID = Emp.getDeptID(name);
int eID = empID.getInt(0);
int dID = DepID.getInt(0);

Intent intent = new Intent(MainActivity.this, empDetails.class);
intent.putExtra("empName", Emp.getEmpData(eID).toString());
intent.putExtra("empTitle",Emp.getEmpData(eID).toString());
intent.putExtra("empPhone",Emp.getEmpData(eID).toString());
intent.putExtra("empEmail",Emp.getEmpData(eID).toString());
intent.putExtra("empDept",Emp.getDeptName(dID).toString());
startActivity(intent);
}
});

使用以下代码:

public Employee getEmpData(Integer employeeID)
{
EmpDept = getReadableDatabase();
Integer[] empRow = {employeeID};
Cursor c = EmpDept.rawQuery("Select name, Title, phone, email, dept from Employee where EmpID like ?", new String[]{employeeID.toString()});
Employee employee = new Employee();
if (c != null && c.getCount() > 0 && c.moveToFirst())
{
employee.setName(c.getString(0));
employee.setTitle(c.getString(1));
employee.setPhone(c.getString(2));
employee.setEmail(c.getString(3));
employee.setDept(c.getString(4));
}
c.close();
return employee;
}

您的列表部分:

Intent intent = new Intent(MainActivity.this, empDetails.class);
Employee employee = Emp.getEmpData(eID);
intent.putExtra("empName", employee.getName());
intent.putExtra("empTitle",employee.getTitle());
intent.putExtra("empPhone",employee.getPhone());
intent.putExtra("empEmail",employee.getEmail());
intent.putExtra("empDept",employee.getDept());

Employee是一个对象,包括姓名、头衔、电话、电子邮件、dept及其getter和setter。

最新更新