【发布时间】:2015-05-26 13:53:09
【问题描述】:
在 Xamarin 项目中,Android - 我是 android 开发新手。在处理活动时,在OnCreate 方法中为ListView 设置自定义适配器。
protected async override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.Main);
var listAdapter = new CustomListAdapter(this);
//.................................................
listView = (ListView) FindViewById(Resource.Id.list);
// populate the listview with data
listView.Adapter = listAdapter;
}
在适配器的ctor 中,创建异步调用中的项目列表。
public CustomListAdapter(Activity context) //We need a context to inflate our row view from
: base()
{
this.context = context;
// items is List<Product>
items = await GetProductList();
}
由于 Getproducts 是异步调用,它会异步加载数据。
问题是一旦我将适配器设置为列表,它将尝试调用适配器的GetView 方法。届时,将不会加载项目。所以有一个空异常。
如何处理这种情况。
谢谢。
【问题讨论】:
-
我从未使用过 xamarin,所以我不知道这是否适用,但它应该 stackoverflow.com/questions/19039868/androidsimpleadapter-error/…
标签: c# android asynchronous xamarin async-await