使用可过滤的列表视图时,我偶尔遇到问题。在过滤食谱列表视图时,我很少会在从我的搜索视图中删除文本时遇到运行时错误。当使用软键盘快速删除文本时,往往会发生这种情况。
90%的时间,此实现完全按照我的预期工作,但在快速删除字符时,我收到以下错误。
04-07 11:38:55.221 9591-9591/brd.cms.sup E/dalvikvm﹕ >>>>> Normal User
04-07 11:38:55.221 9591-9591/brd.cms.sup E/dalvikvm﹕ >>>>> brd.cms.sup [ userId:0 | appId:10359 ]
04-07 11:38:59.295 9591-9591/brd.cms.sup E/OpenGLRenderer﹕ GL_INVALID_OPERATION
04-07 11:40:58.018 9591-9591/brd.cms.sup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: brd.cms.sup , PID: 9591
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131493035, class android.widget.ListView) with Adapter(class brd.cms.sup.RecipeAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1566)
at android.widget.AbsListView.onLayout(AbsListView.java:2598)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15860)
at android.view.ViewGroup.layout(ViewGroup.java:4902)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2379)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2092)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6640)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
下面是我的SearchView.OnQueryTextListener实现。
@Override
public boolean onQueryTextChange(String newText) {
if (recipeAdapter != null) {
try {
recipeAdapter.getFilter().filter(newText);
}
catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
这是我在RecipeAdapter中的私人RecipeFilter。
private class RecipeFilter extends Filter{
List<Recipe> filteredList = new ArrayList<>();
@Override
protected FilterResults performFiltering(CharSequence constraint){
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
filteredList.clear();
if (constraint != null && constraint.toString().length() > 0) {
for (Recipe r : backupList) {
if (r.contains(constraint)) {
filteredList.add(r);
}
}
} else {
filteredList.addAll(backupList);
}
result.count = filteredList.size();
result.values = filteredList;
return result;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
recipeList = (List) results.values;
if (results != null) {
notifyDataSetChanged();
}
else{
notifyDataSetInvalidated();
}
}
}
任何想法都将不胜感激!
答案 0 :(得分:1)
了解您如何使用filteredList
和recipeList
会有所帮助。但是,ListView
失去同步可能是因为缺乏同步。 performFiltering()
方法在后台线程上执行。您必须同步Lists
上的任何突变...而不仅仅是在performFiltering()
方法中,而是在整个适配器中。顺便说一下,我确定您的自定义适配器不是ArrayAdapter
的扩展名。这本身就是另一个问题。
以下是short blog post讨论使用Android ArrayAdapter
进行过滤的问题。最后,您将找到示例代码,显示为适配器编写Filter
类的好方法。基本上你在这里发布的等效代码。
管理列表
总结一下,您有一个recipeList
,它包含适配器中的所有项目或仅包含已过滤的项目。您还有backupList
存储所有项目。然后在Filter
课程中,您有一个filterList
,用于将过滤后的数据转化为recipeList
。
这种方法相当标准......两个主要列表用于跟踪过滤数据和原始数据。使用这些列表的关键是同步并知道使用哪一个。
对于任何getter,您的适配器应始终使用recipeList
中的数据。例如,getItem()
,getItemId()
等。无需同步访问此数据。
对于任何setter,您将执行以下操作
notifyDataSetChanged
对于#2,根据您的设置,无论您的backupList
应该更新。您的recipeList
是否也会更新取决于您是否希望在过滤期间应用突变。
最后,您的过滤器类需要添加同步。特别是当您将数据从backupList
复制到filteredList
时。此外,filteredList
必须是方法performFiltering()
的本地方法,而不是类的全局方法。要么是那个,要么同步整个方法。由于该方法在后台线程上发生,因此多个线程很可能串联执行该方法。这意味着他们都在改变相同的filteredList
......这是不好的。
同样在publishResults
中,您需要同步更新recipeList
,并且当结果为空或为空时,您需要调用notifyDataSetInvalidated()
。