Android - 在AutoCompleteTextView中使用SimpleCursorAdapter会产生异常

时间:2015-07-23 09:40:45

标签: exception simplecursoradapter autocompletetextview

您好我正在开发一个Android应用程序,我正在使用AutoCompleteTextView使用SimpleCursorAdapter搜索名称,如下面的代码所示。

    autoCompleteNamadtxt = (AutoCompleteTextView) findViewById(R.id.namadtxt);
    dbHelper = new DBHelper(this);
    dbHelper.open();

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line,
                                    null,
                                    new String[] { "Namad" },
                                    new int[] {android.R.id.text1});
    autoCompleteNamadtxt.setAdapter(adapter);

    adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
        @Override
        public CharSequence convertToString(Cursor cursor) {
            final int colIndex = cursor.getColumnIndexOrThrow("Namad");
            return cursor.getString(colIndex);
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            String partial = null;
            if (constraint != null)
                partial = constraint.toString();
            Cursor cursor = dbHelper.getAllNamad(new String[]{partial.trim() + "%"});
            startManagingCursor(cursor);
            cursor.moveToFirst();
            return cursor;
        }
    });

    dbHelper.close();

dbHelper:

    public Cursor getAllNamad(String[] param, SQLiteDatabase sqLite) {
    Cursor cursor = sqLite.rawQuery("select Id as _id,Namad from Namad Where Namad LIKE ؟", param);
    return cursor;
}

当我开始输入AutoCompleteTextView时,我面对以下ecxeption,我想知道是否有人可以帮助我。

07-23 09:00:56.209    2398-2419/? W/Filter﹕ An exception occured during performFiltering()!
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.sahmchin.sabadyab/databases/Sabadyab.db
        at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
        at com.sahmchin.sabadyab.Database.TblNamad.getAllNamad(TblNamad.java:32)
        at com.sahmchin.sabadyab.Database.DBHelper.getAllNamad(DBHelper.java:345)
        at com.sahmchin.sabadyab.BuySahmActivity$2.runQuery(BuySahmActivity.java:71)
        at android.widget.CursorAdapter.runQueryOnBackgroundThread(CursorAdapter.java:395)
        at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.os.HandlerThread.run(HandlerThread.java:61)

1 个答案:

答案 0 :(得分:0)

找到自己的方式,我必须把" dbHelper.open()"在" setFilterQueryProvider"的开头功能

        adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            dbHelper.open();
            Cursor cursor = dbHelper.getAllNamad(
                    (constraint != null ? constraint.toString() : null));
            startManagingCursor(cursor);
            cursor.moveToFirst();
            return cursor;
        }
    });