OnCreateView()在OnCreate()完成之前发生,导致

问题描述 投票:0回答:1

我有一个Android片段,它实际上显示了一个自定义ListView。我的片段看起来像这样:

public class ResultSummaryFragment : Android.Support.V4.App.Fragment
    {
        private List<ResultSummary> data;

        public override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            ISharedPreferences preferences = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private);
            string id = preferences.GetString("ID", string.Empty);

            if (id != null)
            {
                RunnerData RunnerData = new RunnerData(id);
                // This is not happening before OnCreateView?
                data = await RunnerData.GetAllResults();
            }
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            Context context = Application.Context;

            View view = inflater.Inflate(Resource.Layout.results_summary_view, container, false);
            ListView listview = view.FindViewById<ListView>(Resource.Id.AllResultsListView);

            //data is being passed in as null...
            ResultsSummaryListAdapter adapter = new ResultsSummaryListAdapter(context, data);
            listview.Adapter = adapter;

            return view;
        }
    }

问题在于,传递到dataResultsSummaryListAdapter为空,因为OnCreateView()代码似乎在data = await RunnerData.GetAllResults();返回之前就已执行。我绝对确定这确实会返回数据(由于下载时间可能会花费一些时间)。

我在这里做错了什么?

我觉得更好的方法是实例化片段,开始获取数据,然后触发以下代码:

ResultsSummaryListAdapter adapter = new ResultsSummaryListAdapter(context, data);
listview.Adapter = adapter;

我不确定如何在Android中执行此操作。

c# android xamarin xamarin.android
1个回答
0
投票

我没有看到在您的OnCreate方法中进行API调用的目的,因为您没有使用它。尝试将其移至OnCreateView方法。我已经更改了数据条件(空检查),只是为了确保API调用仅发生一次。

public class ResultSummaryFragment : Android.Support.V4.App.Fragment
{
    private List<ResultSummary> data;

    public override async View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Context context = Application.Context;

        View view = inflater.Inflate(Resource.Layout.results_summary_view, container, false);
        ListView listview = view.FindViewById<ListView>(Resource.Id.AllResultsListView);

        ISharedPreferences preferences = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private);
        string id = preferences.GetString("ID", string.Empty);

        if (id != null && data == null)
        {
            RunnerData RunnerData = new RunnerData(id);
            // This is not happening before OnCreateView?
            data = await RunnerData.GetAllResults();
        }

        //Now, your compiler knows that you need data and it will wait until data is obtained.
        ResultsSummaryListAdapter adapter = new ResultsSummaryListAdapter(context, data);
        listview.Adapter = adapter;

        return view;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.