当前位置: 首页 > 面试题库 >

在Android阵列适配器中使用notifyDataSetChanged时出错

丁宏浚
2023-03-14
问题内容

11-06 19:52:25.958:E /
AndroidRuntime(29609):java.lang.IllegalStateException:适配器的内容已更改,但ListView没有收到通知。确保不从后台线程修改适配器的内容,而仅从UI线程修改。[在ListView(-1,类android.widget.ListPopupWindow
$
DropDownListView)和Adapter(类com.example.parkfoxxlight_android.PlacesAutoCompleteAdapter)中]

完整日志:http :
//pastebin.com/Hx7k28Rm

适配器的完整代码:http :
//pastebin.com/TfH1bXE3我使用的是https://developers.google.com/places/training/autocomplete-
android中的示例,它具有相当的默认代码,因此似乎有一个Google代码中的错误?

只有在出现上述错误消息的情况下,应用程序才会崩溃。

protected void publishResults(CharSequence constraint,
        FilterResults results) {

    if (results != null && results.count > 0) {
        notifyDataSetChanged();
    } else {
        notifyDataSetInvalidated();
    }
}

活动http://pastebin.com/FYzYtvXY:

public class CityActivity extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.city);

            AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autocomplete_city);

            PlacesAutoCompleteAdapter ad = new PlacesAutoCompleteAdapter(this);
            ProgressBar b = (ProgressBar)findViewById(R.id.progressBar1);
            ad.setLoadingIndicator(b);

            autoCompView.setAdapter(ad);
        }
}

任何想法如何解决这一问题?我在android 4.3上。


问题答案:

FilterperformFiltering()方法在后台线程,并从该方法运行你改变了resultList上您的适配器的基础。如果您更改了该数据列表,并且在那段时间ListView访问了适配器,它将看到某些内容在其不知情的情况下发生了更改(并且不会感到高兴)。您应该避免使用resultListin
performFiltering方法,而只需创建一个新的临时列表:

// in the performFiltering method which runs on a background thread:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
     FilterResults filterResults = new FilterResults();
     ArrayList<String> queryResults;
     if (constraint != null && constraint.length() > 0) {
         queryResults = autocomplete(constraint);
     } else {
         queryResults = new ArrayList<String>(); // empty list/no suggestions showing if there's no valid constraint
     }
     filterResults.values = queryResults;
     filterResults.count = queryResults.size();
     return filterResults; // ## Heading ##
}

private List<String> autocomplete(String input) {
   // don't use the here the resultList List on which the adapter is based!
   // some custom code to get items from http connection
     ArrayList<String> queryResults = new ArrayList<String>(); // new list
     queryResults.add("Some String");
     return queryResults;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
     // update the data with the new set of suggestions
     resultList = (ArrayList<String>)results.values;
     if (results.count > 0) {
         notifyDataSetChanged();
     } else {
         notifyDataSetInvalidated();
     }
}


 类似资料:
  • 我延长了 当我打电话的时候: 什么也没发生。 刷新视图的唯一方法是再次设置适配器(请参见此答案): 我对此解决方案有两个问题: 当我再次设置适配器时,我可以看到屏幕上有一个闪烁 listview返回第一个位置。 有什么想法吗?

  • 本文向大家介绍浅谈Android中适配器的notifyDataSetChanged()为何有时不刷新,包括了浅谈Android中适配器的notifyDataSetChanged()为何有时不刷新的使用技巧和注意事项,需要的朋友参考一下 学过Android开发的人都知道,ListView控件在开发中经常遇到,并且ListView通常结合Adapter适配器来进行数据显示和数据更新操作。姑且假设数据存

  • 我正在从Firebase数据库获取一些数据,并试图用它填充适配器。在适配器的被调用后,屏幕闪烁,什么也没发生,我甚至无法在onBindViewHolder中捕捉到断点。 这是我的代码: POJO类: } 这是我的活动布局,称为“活动结果”。包含RecyclerView的xml 这是我的Adapters ViewHolder布局,名为score_view_holder。xml 因此,它将包含两个水平

  • 我正在创建一个使用RecyclerView显示的卡片列表,其中每个卡片都有一个按钮,可以从列表中删除该卡片。 是否有人有使用notifyItemRemoved()的经验,并且知道它的行为与NotifyDataSetChanged不同的原因? 下面是我正在使用的一些代码:

  • 我正在提高应用程序的稳定性和性能,但现在我被困在Android Studio的警告中。请考虑以下适配器类: 适配器和过滤器工作正常,但是请注意< code>publishResults函数。Android Studio警告说,关于< code > notifyDataSetChanged 。 但是,我不知道如何在这个实例中使用< code > notifyDataSetChanged (带有过滤器

  • 本节将会引入一个全新的概念——适配器,这个名字很形象,和电源适配器的功能类似,从程序设计的角度出发,它可以将不同类型、不同结构的数据适配到一起。 在 Android 中,适配器是 UI 组件和数据之间的桥梁,它帮助我们将数据填充到 UI 组件当中,实现了一个典型的 MVC 模式。我们可以分别编写独立的 UI 样式和数据模型,至于数据如何与 UI 组件绑定都由 Adapter 帮我们完成,这样的好处