对自动完成执行过滤

发布于 2024-11-04 06:37:40 字数 3148 浏览 5 评论 0原文

我有一个自动完成功能,当我开始输入时,我会显示数据库中两列的内容。

现在,我想对此执行过滤,并实现了以下方法:


    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

            String filter="";
            if(constraint==null) filter="";

            else filter=constraint.toString();

          Cursor cursor=db.getCursor(filter);
          return cursor;
        }

我的类中操作数据库的方法如下所示:


 public Cursor getCursor(String prefix) {


        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();


        String[] args = {prefix};


        queryBuilder.setTables(TABLE_2);


      String[] asColumnsToReturn = new String[] {"route","user_id"};


        Cursor mCursor = queryBuilder.query(db,asColumnsToReturn,null, args, null, null,null);


        int idcol =  mCursor.getColumnIndexOrThrow(KEY_ROUTE);

        int kwcol =  mCursor.getColumnIndexOrThrow(KEY_USER_ID );


        while(mCursor.moveToNext()) {

            String id = mCursor.getString(idcol);

            String kw = mCursor.getString(kwcol);

            Log.d(id,kw);
        }

        return mCursor;
    }

问题:为什么我的自动完成中没有显示任何内容???

这就是我的 logcat 的样子:



05-01 09:28:59.207: WARN/Filter(366): An exception occured during performFiltering()!

05-01 09:28:59.207: WARN/Filter(366): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x3114b8

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)


05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:178)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:190)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:55)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1220)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:316)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:266)

05-01 09:28:59.207: WARN/Filter(366):     at com.server.DBAdapter.getCursor(DBAdapter.java:199)

05-01 09:28:59.207: WARN/Filter(366):     at com.server.Server8$ContactListCursorAdapter.runQueryOnBackgroundThread(Server8.java:185)

05-01 09:28:59.207: WARN/Filter(366):     at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
05-01 09:28:59.207: WARN/Filter(366):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
05-01 09:28:59.207: WARN/Filter(366):     at 
android.os.Handler.dispatchMessage(Handler.java:99)

05-01 09:28:59.207: WARN/Filter(366):     at android.os.Looper.loop(Looper.java:123)

05-01 09:28:59.207: WARN/Filter(366):     at 
android.os.HandlerThread.run(HandlerThread.java:60)

I have an autocomplete in which as I start typing I get displayed the content of two columns in a database.

Now,I wanna perfom a filtering on this and I implemented the following metod:


    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

            String filter="";
            if(constraint==null) filter="";

            else filter=constraint.toString();

          Cursor cursor=db.getCursor(filter);
          return cursor;
        }

The method in my class that manipulates the database looks like this:


 public Cursor getCursor(String prefix) {


        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();


        String[] args = {prefix};


        queryBuilder.setTables(TABLE_2);


      String[] asColumnsToReturn = new String[] {"route","user_id"};


        Cursor mCursor = queryBuilder.query(db,asColumnsToReturn,null, args, null, null,null);


        int idcol =  mCursor.getColumnIndexOrThrow(KEY_ROUTE);

        int kwcol =  mCursor.getColumnIndexOrThrow(KEY_USER_ID );


        while(mCursor.moveToNext()) {

            String id = mCursor.getString(idcol);

            String kw = mCursor.getString(kwcol);

            Log.d(id,kw);
        }

        return mCursor;
    }

Question:Why don't I get nothing displayed in my autocomplete???

This is how my logcat looks like:



05-01 09:28:59.207: WARN/Filter(366): An exception occured during performFiltering()!

05-01 09:28:59.207: WARN/Filter(366): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x3114b8

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)


05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:178)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:190)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:55)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1220)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:316)

05-01 09:28:59.207: WARN/Filter(366):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:266)

05-01 09:28:59.207: WARN/Filter(366):     at com.server.DBAdapter.getCursor(DBAdapter.java:199)

05-01 09:28:59.207: WARN/Filter(366):     at com.server.Server8$ContactListCursorAdapter.runQueryOnBackgroundThread(Server8.java:185)

05-01 09:28:59.207: WARN/Filter(366):     at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
05-01 09:28:59.207: WARN/Filter(366):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
05-01 09:28:59.207: WARN/Filter(366):     at 
android.os.Handler.dispatchMessage(Handler.java:99)

05-01 09:28:59.207: WARN/Filter(366):     at android.os.Looper.loop(Looper.java:123)

05-01 09:28:59.207: WARN/Filter(366):     at 
android.os.HandlerThread.run(HandlerThread.java:60)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-11-11 06:37:40

尝试在 while 循环之前调用 moveToFirst()

mCursor.moveToFirst()
 while(mCursor.moveToNext()) {
         String id = mCursor.getString(idcol);

        String kw = mCursor.getString(kwcol);

        Log.d(id,kw);
    }

try and call moveToFirst() before the while loop

mCursor.moveToFirst()
 while(mCursor.moveToNext()) {
         String id = mCursor.getString(idcol);

        String kw = mCursor.getString(kwcol);

        Log.d(id,kw);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文