0

Something is wrong with the cursor carrying data issue, debugging, i got that "java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.".

But i still can't adjust the cursor issue, i know that connection shouldn't be closed before cursor returning data, but i can't figure out how to adjust it.

I want to take some info from getEmpData method, and then pass it.

my method:

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;

        }

List view part:

When name is clicked it should move the data to new activity.

       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);
              }
          });
  • Please stop reposting this question. As I mentioned last time, simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. – Mike M. Nov 29 '18 at 07:09

1 Answers1

0

use this code :

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;

        }

your list part :

    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());

and Employee is an object including name, title, phone, email, dept and their getter and setters.

  • Why can't i use it, without employee object part? – AspiringLilly Nov 28 '18 at 15:24
  • @AspiringLilly first you are closing the db and returning it’s cursor after, so cursor will become not usable. Second it is not right to return cursor to your activity. Third the way you are using the cursor is wrong ;) – fatemeh fallahi arezoudar Nov 28 '18 at 15:32
  • Then how should i adjust it so it could return to the activity, why it is not right to return it there? i didn't get it – AspiringLilly Nov 28 '18 at 19:40
  • @AspiringLilly the internals of a cursor can contain lots of resources so it is important to clean up the cursor after handling it appropriately, there for it is better to extract data from the cursor and save it to an object after that close the cursor and then return the object to your activity. – fatemeh fallahi arezoudar Nov 28 '18 at 20:23