gpt4 book ai didi

java - 实现自己的 Android CursorAdapter 用于搜索建议 - 未知异常

转载 作者:行者123 更新时间:2023-12-01 22:24:57 25 4
gpt4 key购买 nike

我已经实现了自己的光标适配器来手动处理建议,这样我就可以摆脱android文档提出的过度杀伤方法。基本上我所做的就是在操作栏搜索 View 膨胀后将其 setOnQueryTextListener 。每次用户输入新的搜索文本时,我都会查询返回游标的 Sqlite 数据库。最后,我使用检索到的游标创建自己的游标适配器,并使用 setSuggestionsAdapter 方法将其设置为 searchview。这里的问题是,我收到许多未知的异常,具体取决于每次配置更改时更新光标的方式。我稍后再解释,首先是一些代码:

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

公共(public)类 BusStopCursorAdapter 扩展 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;

}
}

************************ 监听器添加到 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;
}
});

在这种情况下,我更新光标所做的就是大幅添加一个全新的适配器。起初一切都很顺利,但是当配置发生更改时,我收到以下日志:

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

该应用程序似乎运行良好。唯一无法解释的问题(可能与这些异常有关)是,在配置更改后,有时会出现建议,有时则不会。就像它无法执行查询或其他操作一样。

例如,如果我使用这个替代监听器实现,它只更新游标(因为它应该是最干净的方式)而不是重新创建整个适配器,那么事情会变得更糟:我收到未知的 SQL 错误(该错误不应该出现,因为sql 查询工作完美),某种游标列访问错误(在显示建议时不应该出现),并且应用程序最终崩溃。

**************************** 另类监听器******************** **************

        @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;
}

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

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

我希望得到一些帮助,因为在花费数小时的搜索后我完全陷入困境。提前致谢。

最佳答案

你把这个搞得太复杂了:不需要setOnQueryTextListener,只需调用:

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

onCreateOptionsMenu中,您将需要您的Activity来实现FilterQueryProvider,其中在其runQuery中返回您的Cursor有建议

关于java - 实现自己的 Android CursorAdapter 用于搜索建议 - 未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28967136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com