C# 在片段内调用REST服务并在回收视图内填充数据
我是Xamarin Android的初学者。我正在尝试构建一个使用REST服务从服务器获取数据的应用程序。 我试图在一个片段中调用web服务,该片段使用recycler视图列出数据的内容。问题是,对客户机函数的调用是在调用recycler适配器类之后进行的,因此不会填充数据 下面是我的片段类的代码:C# 在片段内调用REST服务并在回收视图内填充数据,c#,android,asp.net,asp.net-web-api,xamarin,C#,Android,Asp.net,Asp.net Web Api,Xamarin,我是Xamarin Android的初学者。我正在尝试构建一个使用REST服务从服务器获取数据的应用程序。 我试图在一个片段中调用web服务,该片段使用recycler视图列出数据的内容。问题是,对客户机函数的调用是在调用recycler适配器类之后进行的,因此不会填充数据 下面是我的片段类的代码: public override void OnCreate(Bundle savedInstanceState) { base.OnCreat
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
notifications = new List<Notification_Struct>();
mClient = new WebClient();
Url = new Uri("http://10.71.34.1:63026/api/notifications/PetePentreath");
mClient.DownloadDataAsync(Url);
mClient.DownloadDataCompleted+= MClient_DownloadDataCompleted;
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
View view = inflater.Inflate(Resource.Layout.recyclerviewcontainer, container, false);
notifitem = view.FindViewById<RecyclerView>(Resource.Id.rv);
notifitem.HasFixedSize = true;
layoutmanager = new LinearLayoutManager(Activity);
adapter = new NotificationAdapter(notifications);
notifitem.SetLayoutManager(layoutmanager);
notifitem.SetAdapter(adapter);
return view;
}
void MClient_DownloadDataCompleted (object sender, DownloadDataCompletedEventArgs e)
{
string json = Encoding.UTF8.GetString(e.Result);
notifications = JsonConvert.DeserializeObject<List<Notification_Struct>>(json);
}
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
通知=新列表();
mClient=新的WebClient();
Url=新Uri(“http://10.71.34.1:63026/api/notifications/PetePentreath");
mClient.DownloadDataAsync(Url);
mClient.DownloadDataCompleted+=mClient\u DownloadDataCompleted;
//在这里创建你的片段
}
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
//使用此选项可返回此片段的自定义视图
//返回充气器。充气(Resource.Layout.YourFragment,container,false);
视图=充气机.充气(Resource.Layout.RecycleServiceContainer,container,false);
notifitem=view.findviewbyd(Resource.Id.rv);
notifitem.HasFixedSize=真;
layoutmanager=新的LinearLayoutManager(活动);
适配器=新通知适配器(通知);
通知项SetLayoutManager(layoutmanager);
通知项设置适配器(适配器);
返回视图;
}
void MClient_DownloadDataCompleted(对象发送方,DownloadDataCompletedEventArgs e)
{
string json=Encoding.UTF8.GetString(e.Result);
notifications=JsonConvert.DeserializeObject(json);
}
}
我在OnCreate中调用web服务,因为在OnCreateView中调用它不会触发DownloadDataCompleted事件。
我希望在调用适配器类之前触发此事件,以便通知变量具有要传递给recycler视图适配器的数据。我如何做到这一点
感谢您的帮助
谢谢 简而言之:
您需要等待请求完成,或者需要在请求完成后重置适配器。我建议阅读async/await
:
i、 e
void MClient\u DownloadDataCompleted(对象发送方,DownloadDataCompletedEventArgs e)
{
string json=Encoding.UTF8.GetString(e.Result);
notifications=JsonConvert.DeserializeObject(json);
//获取您的RecyclerView的引用
//使用通知设置适配器
recyclerView=(recyclerView)FindViewById(R.id.myRecyclerView);//或保留以前的引用
适配器=新通知适配器(通知);
recyclerView.setAdapter(适配器);
}
在连接适配器之前,您需要等待请求完成。您可以在第一次设置空适配器。然后在MClient\u DownloadDataCompleted
中,您可以再次设置适配器。否则,我会转而研究async/await
实践。@Jon Douglas如何用代码实现async await?这是一个相当不错的教程。好的,谢谢!:)
void MClient_DownloadDataCompleted (object sender, DownloadDataCompletedEventArgs e)
{
string json = Encoding.UTF8.GetString(e.Result);
notifications = JsonConvert.DeserializeObject<List<Notification_Struct>>(json);
//Get a reference to your RecyclerView
//Set the adapter with your notifications
recyclerView = (RecyclerView) FindViewById(R.id.myRecyclerView); //Or keep a reference from before
adapter = new NotificationAdapter(notifications);
recyclerView.setAdapter(adapter);
}