DataBase ObjectClosedException从Android中的SQLite填充listview

时间:2014-07-16 05:27:00

标签: android android-sqlite

在这里输入图像描述我真的很困惑数据库对象未关闭的地方。我正在尝试从SQLite填充listview。当我运行List视图时为空并获得异常。

有人可以帮我解决问题所在。谢谢你。

这是我的代码

public class Employee_List extends Activity
{

    Context mContext;
    Employee emp;

    ImageView imgButtonBack;

    protected ListAdapter adapter;
    ListView listEmployee;
     SimpleCursorAdapter mAdapter;

 public static final String EMPLOYEE_DETAILS_TABLE = "employee_details";
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staff_employee_list);
        listEmployee = (ListView)findViewById(R.id.mylist);
        showList();
    }

    private void showList()
    {     

    ArrayList<Employee> EmployeeList = new ArrayList<Employee>();  
    EmployeeList.clear();  

    String strQuery = "SELECT sum(empid) as emp_id, Staff_emp_name as emp_name, department as emp_dept, designation as emp_designation,"
                    +" (SUM(performance_rate_one+performance_rate_two+performance_rate_three+performance_rate_four+performance_rate_five)/5) as TotalPerformance"
                    +" FROM employee_details INNER JOIN performance"
                    +" ON employee_details.Emp_id = performance.empid "
                    +" GROUP BY empid "
                    +" ORDER BY TotalPerformance DESC";

    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
    Cursor c1 =sqlDatabase.rawQuery(strQuery, null);


    if (c1 != null && c1.getCount() != 0) 
    {   
        if (c1.moveToFirst())

        {   
            do
            {    
                Employee EmployeeListItems = new Employee();  
                EmployeeListItems.setEmployeeId(c1.getInt(c1.getColumnIndex("emp_id")));
                EmployeeListItems.setName(c1.getString(c1.getColumnIndex("emp_name")));
                EmployeeListItems.setDepartment(c1.getString(c1.getColumnIndex("emp_dept")));
                EmployeeListItems.setDesignation(c1.getString(c1.getColumnIndex("emp_designation")));

                EmployeeListItems.setPerformancerate(c1.getString(c1.getColumnIndex("TotalPerformance")));
                EmployeeList.add(EmployeeListItems);     

            }while (c1.moveToNext());   
            c1.close();
            sqlDatabase.close();

        } 

    } 

    EmployeeList_Adapter contactListAdapter = new EmployeeList_Adapter(Employee_List.this, EmployeeList); 
    listEmployee.setAdapter(contactListAdapter);  
    //contactListAdapter.notifyDataSetChanged();

    }

}

ListAdapter类

public class EmployeeList_Adapter extends BaseAdapter
        {
            Context context;
            ArrayList<Employee> Employee_List;
            public EmployeeList_Adapter(Context context,
                    ArrayList<Employee> employee_List) {
                super();
                this.context = context;
                Employee_List = employee_List;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return Employee_List.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return Employee_List.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup arg2) {
                // TODO Auto-generated method stub
                Employee EmployeeListItems = Employee_List.get(position);
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.staff_employee_list_item, null);
                }


                TextView tvID = (TextView) convertView.findViewById(R.id.textViewStaff_emp_Id);
                tvID.setText(EmployeeListItems.getEmployeeId()+"");

                ImageView imgProfile = (ImageView) convertView.findViewById(R.id.imgStaff_Emp);


                TextView tvName = (TextView) convertView.findViewById(R.id.textViewStaff_Emp_ame);
                tvName.setText(EmployeeListItems.getName());

                TextView tvDepartment = (TextView) convertView.findViewById(R.id.textViewStaff_Emp_Department);
                tvDepartment.setText(EmployeeListItems.getDepartment());


                TextView tvDesignation = (TextView) convertView.findViewById(R.id.textViewStaff_Emp_Designation);
                tvDesignation.setText(EmployeeListItems.getDesignation());


                TextView tvPerformanceRate = (TextView) convertView.findViewById(R.id.empLoyeeRate);
                tvPerformanceRate.setText(EmployeeListItems.getPerformancerate());

                final Employee empPOsition = Employee_List.get(position);
                int positionA = Employee_List.indexOf(empPOsition);
                System.out.println(" empPOsition = " + positionA);

                return convertView;
            }

        }

这是Log Cat INfo

07-16 10:36:41.691: E/Database(383): close() was never explicitly called on database '/data/data/com.sqlitedemo/databases/EmployeeReview.db' 
07-16 10:36:41.691: E/Database(383): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-16 10:36:41.691: E/Database(383):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
07-16 10:36:41.691: E/Database(383):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
07-16 10:36:41.691: E/Database(383):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
07-16 10:36:41.691: E/Database(383):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
07-16 10:36:41.691: E/Database(383):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
07-16 10:36:41.691: E/Database(383):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-16 10:36:41.691: E/Database(383):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
07-16 10:36:41.691: E/Database(383):    at com.sqlitedemo.Employee_List.showList(Employee_List.java:147)
07-16 10:36:41.691: E/Database(383):    at com.sqlitedemo.Employee_List.onCreate(Employee_List.java:62)
07-16 10:36:41.691: E/Database(383):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-16 10:36:41.691: E/Database(383):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-16 10:36:41.691: E/Database(383):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-16 10:36:41.691: E/Database(383):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-16 10:36:41.691: E/Database(383):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-16 10:36:41.691: E/Database(383):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 10:36:41.691: E/Database(383):    at android.os.Looper.loop(Looper.java:123)
07-16 10:36:41.691: E/Database(383):    at android.app.ActivityThread.main(ActivityThread.java:3683)
07-16 10:36:41.691: E/Database(383):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 10:36:41.691: E/Database(383):    at java.lang.reflect.Method.invoke(Method.java:507)
07-16 10:36:41.691: E/Database(383):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-16 10:36:41.691: E/Database(383):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-16 10:36:41.691: E/Database(383):    at dalvik.system.NativeStart.main(Native Method)

enter image description here

1 个答案:

答案 0 :(得分:0)

每次设置列表时都不应该打开数据库,首先在应用启动时打开数据库。假设如果关闭db的if语句在再次设置列表时没有运行,则会因为您再次打开已经打开的数据库而引发错误。