Android - 在 AutoCompleteTextView 中使用 SimpleCursorAdapter 会出现异常

Android - Using SimpleCursorAdapter in AutoCompleteTextView gives Exception

您好,我正在开发一个 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();

数据库助手:

    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 时,我遇到了以下异常,我想知道是否有人可以帮助我。

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.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)

我自己找到了方法,我必须把 "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;
        }
    });