eclipse 在片段和自定义适配器中更新/刷新列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11460194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:33:59  来源:igfitidea点击:

Update/refresh listview in a fragment & custom adapter

androideclipselistviewrefreshfragment

提问by user1521841

What do I need to call from Fragment 1 to force an update/refresh of Fragment 2 when Fragment 2 has code in onActivityCreated()to load the data, create a custom array adapter and set it on a ListView?

当片段 2 有代码onActivityCreated()加载数据、创建自定义数组适配器并将其设置在 ListView 上时,我需要从片段 1 调用什么来强制更新/刷新片段 2 ?

Is there a way to update/refresh the ListView or is the intent to Replace()the Fragment to force it to be rebuilt and rerun the code in onActivityCreated()?

有没有办法更新/刷新 ListView 或者Replace()Fragment的意图是强制它重建并重新运行代码onActivityCreated()

Here is a snipped of the Fragment 2 onActivityCreated() code:

这是片段 2 onActivityCreated() 代码的片段:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
      super.onActivityCreated(savedInstanceState);

      // Get the prepared data and load it into this view
      ArrayList<ReportData> oReportResultsData = Report.LoadData(Home.ScheduleJSonData,         Home.SelectedDate);

      //Set the list into the gridview (Listview formatted with rows/columns)
      ScheduledActivityReportGridArrayAdapter myGridArrayAdapter = new         ScheduledActivityReportGridArrayAdapter(this.getActivity(), R.layout.homeschedulerow, oReportResultsData);
      lstReport.setAdapter(myGridArrayAdapter);
    }

I did try to manually call notifyDataSetChanged()on the Fragment Manager in hopes that would force it to reload/refresh Fragment 2. I have reread the docs and many great answers out here but the custom LoadData()method and custom adapter being setup in onActivityCreated()I've not see addressed.

我确实尝试手动调用notifyDataSetChanged()Fragment Manager,希望能强制它重新加载/刷新 Fragment 2。我已经重新阅读了文档和许多很棒的答案,但是我没有看到LoadData()正在设置的自定义方法和自定义适配器onActivityCreated().

Many Thanks!

非常感谢!

回答by Jan-Henk

Your fragments should communicate with each other through the Activity, see http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity.

您的片段应该通过 Activity 相互通信,请参阅http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Your first fragment should call a method on the activity, which in turn calls another method on the second fragment, which will then reload your data.

您的第一个片段应该调用 Activity 上的一个方法,该方法又调用第二个片段上的另一个方法,然后该方法将重新加载您的数据。