从活动更新列表片段
我试图在按下按钮时更新我的列表片段。该按钮将名称放入数据库中,但如何更新列表以便显示该名称? listfragment 使用cursorLoader 从数据库加载。我什至尝试在数据库上使用内容观察器并调用 getLoaderManager().restartLoader() ,但这也不起作用。
那么,当我单击活动中的按钮时,我该如何更新列表呢?我发现这方面的信息很少,请帮忙
I am trying to update my listfragment when a button is pressed. the button puts a name into a database but how do I update the list so that name appears? the listfragment uses a cursorLoader to load from the database. I even tried using a content observer on the database and calling getLoaderManager().restartLoader()
but that didnt work either.
So what can I do to update the list when I click a button in an activity? I have found very little information on this please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定,但我认为您的
Fragment
和Activity
之间的交互发生了一些奇怪的事情,您显然以某种方式同时显示它们。您在活动中侦听按钮有什么原因吗?您正在处理多窗格屏幕或其他东西吗?让 Fragment 自己处理这个问题似乎要简单得多(我认为它是实现 LoaderCallbacks 的)。另外,请确保您正确实现
Fragment
生命周期...即调用initLoader()
并在onActivityCreated()
中设置列表的适配器> 与onCreate()
相反。希望有帮助!
I can't be sure, but I assume there is something weird going on with the interaction between your
Fragment
and yourActivity
, which you are apparently displaying simultaneously somehow. Is there any reason why you are listening for the Button in the Activity? Are you dealing with a multi-pane screen or something? It seems like it'd be a lot simpler to just have the Fragment deal with this itself (since it is the one implementing theLoaderCallbacks
, I assume).Also, be sure that you are implementing your
Fragment
lifecycle correctly... i.e. you callinitLoader()
and set the list's adapter inonActivityCreated()
as opposed toonCreate()
.Hope that helps!