访问片段活动?片段

2023-09-12 23:48:40 作者:社不社会你能咋滴

我有2片段一个主活动。这两个片段具有的ListView 。我想更新的列表中 MainActivity 。有没有什么办法来访问片段列表适配器活动与出使适配器,静态适配器。 目前,我做这样的 Mainactivity.java

 公共无效updatelist(){
  如果(fragmentstate = 0)
    Fragment1.adapter.notifyDataSetChanged();
  其他
    Fragment2.adapter.notifyDataSetChanged();

}
 

解决方案

如果你只是想更新的片段列表,您不必访问adpaters。你可以在片段注册本地播放,并从MainActivity发送本地广播消息。

从frgments,

  LocalBroadcastManager.getInstance(的getContext())registerReceiver(...)。
 
梅艳芳纪念会歌迷泪不止 洪罗拔再提梅姐趣事

从MainActivity,

  LocalBroadcastManager.getInstance(的getContext())。sendBroadcast(...)
 

I have one main activity with 2 fragments. Both fragments have a ListView. I want to update the list in MainActivity. Is there any way to access fragment list adapter in activity with out making adapter as static adapter. Currently I am doing like this in Mainactivity.java

public void updatelist() {
  if(fragmentstate=0)
    Fragment1.adapter.notifyDataSetChanged();
  else
    Fragment2.adapter.notifyDataSetChanged();

}

解决方案

If you just want to update lists on fragments, you don't have to access the adpaters. You can register local broadcast on fragments, and send local broadcast message from MainActivity.

From frgments,

LocalBroadcastManager.getInstance(getContext()).registerReceiver(...);

From MainActivity,

LocalBroadcastManager.getInstance(getContext()).sendBroadcast(...)