I got an issue reported in Xamarin Insights.
Java.Lang.RuntimeExceptionThe content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(-1, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]
I guess this on happens because my maintenance of a global datasource as a list view.
What i'm doing is have a list which is the datasource of a list in one page as the following:
partial class App : Application
{
public static async void ProjectListRequest(Action callBack)
{
IProgressDialog service = dialogService.Loading("Refreshing Projects....",null,"",true);
WebResponse resp = await BaseHttpRequest.SendHttpRequest("api2/service/project/mobile/summaries");
if (resp == null) {
service.Hide();
dialogService.Alert("Error","Refresh Failed","OK");
return;
}
var serializer = new JsonSerializer();
List<ProjectSumDto> projectListRet = new List<ProjectSumDto>();
using (var sr = new StreamReader(resp.GetResponseStream()))
{
using (var jsonTextReader = new JsonTextReader(sr))
{
JArray jObject = serializer.Deserialize(jsonTextReader) as JArray;
projectListRet = jObject.ToObject<List<ProjectSumDto>>();
}
}
Device.BeginInvokeOnMainThread(()=>{
if (App.ListViewItems.Count > 0) {
App.ListViewItems.Clear ();
}
foreach (ProjectSumDto prj in projectListRet)
App.ListViewItems.Add(prj);
App.ListProject = projectListRet;
});
service.Hide();
if (callBack != null)
callBack ();
}
}
For eg, I have page 1 which is a list like this:
Cell = new DataTemplate(typeof(CustomCell));
App. ProjectListRequest(null);
listView.ItemsSource = App.ListViewItems;
BindingContext = App.ListViewItems;
listView.ItemTemplate = Cell;
every time when this page is load will do the code above. When in page 2 which is the detail page of page 1, I change the status of project and call this function again like this which will update the datasource of page 1 's list. I guess when this service is done or when user go back to page 1, then it crashes.
await Task.Run (() => App.ProjectListRequest (null));
The overall idea is just want to maintain a global datasource which I can update it in any pages not only in the page which is using it.