适配器的内容发生了变化,但 ListView 没有收到通知。安卓问题
Posted
技术标签:
【中文标题】适配器的内容发生了变化,但 ListView 没有收到通知。安卓问题【英文标题】:The content of the adapter has changed but ListView did not receive a notification. android problem 【发布时间】:2020-06-27 06:13:01 【问题描述】:当数据到达 ArrayAdapter 时我的问题列表视图没有更新到最后一个数据我必须转到另一个页面并返回以更新列表视图 问题listview没有自动更新
我在此代码中的操作是当在 firebase 上更新数据时,我将该数据插入 arraylist 并将 arraylist 放入 arrayAdapter 中,然后我启动应用程序它的工作,如果我将另一个数据添加到我的数据库中并转到另一个页面,然后回到我的应用程序上的列表视图,它显示最后的数据没有任何问题,但是当我留在列表视图上时,当新数据出现并触及任何列表视图时,应用程序继续崩溃并且 IllegalStateException 来请帮助我的问题并为我感到抱歉英语不太好 我希望你明白这个问题 我使用 mvvm 设计模式 我的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
groupfragmentview = inflater.inflate(R.layout.fragment_group, container, false);
list_View = (ListView) groupfragmentview.findViewById(R.id.list_view);
//
myview = ViewModelProvider.androidViewModelFactory.getInstance(getActivity().getApplication()).create(group_frg_view.class);
myview.init();
myview.getgroup().observe(this.getActivity(), new Observer<group_frg_model>()
@Override
public void onChanged(final group_frg_model group_frg_model)
updateUI(group_frg_model);
list_View.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
String groupname = parent.getItemAtPosition(position).toString();
String groupid= group_frg_model.getGroupid().get(position);
String TAG=null;
Log.d(TAG,"name " + groupid);
Intent groupintent = new Intent(getContext(),GroupChatActivity.class);
groupintent.putExtra("groupname",groupname);
groupintent.putExtra("groupid",groupid);
startActivity(groupintent);
);
);
return groupfragmentview;
private void updateUI(group_frg_model group_frg_model)
array_Adapter = new ArrayAdapter<>(getActivity(), R.layout.single_row_group_list, R.id.group_name,group_frg_model.getGroupname());
list_View.setAdapter(array_Adapter);
list_View.requestLayout();
array_Adapter.notifyDataSetChanged();
IllegalStateException
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(2131230911, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1618)
at android.widget.AbsListView.onTouchUp(AbsListView.java:4013)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3806)
at android.view.View.dispatchTouchEvent(View.java:9943)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373)
at android.view.View.dispatchPointerEvent(View.java:10163)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
【问题讨论】:
您可以初始化适配器一次,然后只更新列表并调用 notifyDataSetChanged。也切换到 RecyclerView。 【参考方案1】:调用这个array_Adapter.notifyDataSetChanged();
,你的列表已经改变,就像你得到你的 api 响应一样
【讨论】:
我调用array_Adapter.notifyDataSetChanged();数据插入列表视图后,但没有任何变化 array_Adapter = new ArrayAdapter(getActivity(), R.layout.single_row_group_list, R.id.group_name,group_frg_model.getGroupname()); list_View.setAdapter(array_Adapter); list_View.requestLayout(); array_Adapter.notifyDataSetChanged();【参考方案2】:我解决了我的问题谢谢大家搜索后我发现数据被延迟并且列表视图不知道我刚刚的任何数据变化 a 在数据更改时编辑存储库模型,然后不要通知列表或数组列表数据更改
【讨论】:
以上是关于适配器的内容发生了变化,但 ListView 没有收到通知。安卓问题的主要内容,如果未能解决你的问题,请参考以下文章
Android开发中常用的ListView列表的优化方式ViewHolder
在自定义 ListView 适配器中执行 findViewById() 时发生 NullPointerException