搜索建议实施自己的Andr​​oid的CursorAdapter - 未知异常 [英] Implementing own Android CursorAdapter for search suggestions - unknown exceptions

查看:548
本文介绍了搜索建议实施自己的Andr​​oid的CursorAdapter - 未知异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了我自己的光标适配器来手工处理的建议,这样我就可以摆脱Android的文档提出了矫枉过正的方法。
基本上,我要做的就是setOnQueryTextListener的动作条搜索查看它被夸大了。每次用户输入一个新的搜索文字,我查询一个SQLite数据库返回一个指针。最后,我创造我自己的光标适配器与检索到的光标,将其设置为与setSuggestionsAdapter方法​​搜索查看。
这里的问题是,我越来越依赖于我每次更新光标配置的变化并提供了许多未知异常。 I'exaplin后来,第一部分code:

I have implemented my own cursor adapter to manually handle suggestions, so that I can get rid of the overkill method that android docs proposes. Basically what I do is setOnQueryTextListener to the actionbar searchview after it's been inflated. Everytime the user inputs a new search text, I query a Sqlite db which returns a cursor. Finally, I create my own cursor adapter with the retrieved cursor and set it to the searchview with setSuggestionsAdapter method. The problem here is that I am getting many unknown exceptions depending on the way I update the cursor everytime the config changes. I'exaplin it later, first some code:

************************我自己的光标适配器类****************** ******

************************ My own cursor adapter class ************************

公共类BusStopCursorAdapter扩展的CursorAdapter {

public class BusStopCursorAdapter extends CursorAdapter {

public BusStopCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, false);

}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    //set address
    ((TextView)view.findViewById(R.id.address)).setText(cursor.getString(1));
    //set line
    ((TextView)view.findViewById(R.id.line)).setText(cursor.getString(2));

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = inflater.inflate(R.layout.suggestions_layout, parent, false);

    //set address
    ((TextView)view.findViewById(R.id.address)).setText(cursor.getString(1));
    //set line
    ((TextView)view.findViewById(R.id.line)).setText(cursor.getString(2));


    return view;

}
}

***********************监听器添加到搜索查看****************** *

*********************** the listener added to the searchView *******************

        // Associate searchView listeners
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            System.out.println("QUERY STRING: "+newText);
            Cursor cursor = queryData(newText);
            searchView.setSuggestionsAdapter(new BusStopCursorAdapter(getBaseContext(), cursor));

            return true;
        }
    });

在这种情况下,我做什么更新光标大幅度增加一个全新的适配器。起初一切顺利,但是当一个配置的变化发生了,我得到这个日志:

In this case, what I do to update the cursor is drastically add a brand new adapter. At first everything goes well, but when a config change happens I get this log:

03-10 15:20:11.762    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING: ad
03-10 15:20:13.012    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING: ado
03-10 15:20:13.992    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING: adol
03-10 15:20:15.602    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING:
03-10 15:20:15.602    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING: adol
03-10 15:20:15.662    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
03-10 15:20:15.672    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
03-10 15:20:15.722    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:20:15.812    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:20:15.882    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
03-10 15:20:18.402    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING:
03-10 15:20:18.402    1927-1927/com.nikkis.vallabus I/System.out﹕ QUERY STRING: adol
03-10 15:20:18.492    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
03-10 15:20:18.492    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
03-10 15:20:18.522    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:20:18.552    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:20:18.592    1927-1927/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection

应用程序似乎很好地工作。唯一无法解释的问题(可能是对这些例外)是一个配置更改后有时出现的建议,有时不。是一样,如果它不可能执行查询或东西。

The application seems to work fine. The only unexplainable issue (probably regarding to these exceptions) is that after a config change sometimes the suggestions appear and sometimes dont. Is like if it couldnt perform the query or something.

例如,如果我用这个替代监听器实现的更新只是光标(因为它应该是最干净的方式),而不是重新创建整个适配器事情变得更糟:我收到未知的SQL错误(这不应该是存在的SQL查询工作perfeclty),某种光标列访问错误的(或者不应该是那里正在显示的建议)和应用程序最终崩溃。

If for example I use this alternative listener implementation which updates just the cursor (as it should be the cleanest way) instead of recreating the whole adapter the thing gets even worse: I get unknown SQL error (which shouldnt be there as the sql query works perfeclty), some kind of cursor column access error(either shouldnt be there as the suggestions are being shown) and the app eventually crashes.

********替代监听******************** **************

************************* Alternative listener**********************************

        @Override
        public boolean onQueryTextChange(String newText) {
            System.out.println("QUERY STRING: "+newText);
            Cursor cursor = queryData(newText);
         /*   if (newText !="")
            searchView.setSuggestionsAdapter(new BusStopCursorAdapter(getBaseContext(), cursor));*/

            if (searchView.getSuggestionsAdapter() == null) {
                searchView.setSuggestionsAdapter(new BusStopCursorAdapter(getBaseContext(), cursor));
            } else {
                searchView.getSuggestionsAdapter().changeCursor(cursor);
            }

            return true;
        }

*********的崩溃日志************** *******************

****************************** the crashing log*********************************

03-10 15:58:46.762    3767-3767/com.nikkis.vallabus  W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:58:46.792    3767-3767/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
03-10 15:58:48.612    3767-3767/com.nikkis.vallabus I/System.out﹕ QUERY STRING:
03-10 15:58:48.622    3767-3767/com.nikkis.vallabus I/System.out﹕ QUERY STRING: adol
03-10 15:58:48.632    3767-4304/com.nikkis.vallabus W/Filter﹕ An exception occured during performFiltering()!
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT stop.stopID AS _id, stop.address, stop_line.line FROM stop_line, stop WHERE stop.stopID = stop_line.stop AND stop.address LIKE '%%';
        at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
        at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
        at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
        at android.widget.CursorFilter.performFiltering(CursorFilter.java:53)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:213)
        at android.os.HandlerThread.run(HandlerThread.java:60)
03-10 15:58:48.652    3767-3767/com.nikkis.vallabus W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
03-10 15:58:48.652    3767-3767/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
03-10 15:58:48.652    3767-4304/com.nikkis.vallabus E/CursorWindow﹕ Failed to read row 0, column 0 from a CursorWindow which has 1 rows, 0 columns.
03-10 15:58:48.702    3767-3767/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:58:48.792    3767-3767/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
03-10 15:58:48.832    3767-3767/com.nikkis.vallabus W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection

我想AP preciate一些帮助,因为我花的搜索小时后完全被卡住。先谢谢了。

I would appreciate some help as I am completely stuck after spending hours of searching. Thanks in advance.

推荐答案

您已经过于复杂这一点:没有必要为setOnQueryTextListener,只要致电:

you have over-complicated this: there is no need for setOnQueryTextListener, just call:

myCursorAdapter.setFilterQueryProvider(this);
searchView.setSuggestionsAdapter(myCursorAdapter);

onCreateOptionsMenu ,你会需要你的活动实施 FilterQueryProvider ,其中在其 runQuery 你回到你的光标搭配建议

in onCreateOptionsMenu, you will need your Activity to implement FilterQueryProvider where in its runQuery you return your Cursor with suggestions

这篇关于搜索建议实施自己的Andr​​oid的CursorAdapter - 未知异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆