I have discovered a few issues with ListViews in Mono for Android that may be bugs. I have posted a fairly-simple project that demos both of these issues in more detail here:
Example Project
I’m wondering if I’m doing something wrong or if these are legitimate bugs.
First, if you try to use an inflated view as the footer view for a ListView by calling AddFooterView it throws a ClassCastException. Using a dynamically-created view works fine but you can’t apply styles to a dynamically created view.
So this works fine:
goodButton.Click += delegate {
TextView tv = new TextView(this);
tv.Text = "THIS IS THE LIST FOOTER";
listView.AddFooterView(tv);
listView.Adapter = adapter;
Toast.MakeText(this, "Footer was added...", ToastLength.Short).Show();
};
But this throws ClassCastException:
TextView footerViewButton = FindViewById(Resource.Id.listFooterButton) as TextView;
badButton.Click += delegate {
listView.AddFooterView(footerViewButton);
listView.Adapter = adapter;
Toast.MakeText(this, "Custom Footer was moved...", ToastLength.Short).Show();
};
Second, if you create a ListView and set an ItemClick handle, then call NotifyDataSetChanged() on the ListView the ItemClick event is no longer fired:
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.ItemClick);
ListView list = FindViewById(Resource.Id.itemClickList) as ListView;
mAdapter = new CustomAdapter(this, Countries);
list.Adapter = mAdapter;
list.ItemClick += new EventHandler<ItemEventArgs>(list_ItemClick);
}
The CustomAdapter calls this method in the activity when the last view in the list is rendered:
public void GetMoreListItems() {
if (mAdapter.Count < Countries.Length + MoreCountries.Length) {
string[] allCountries = new string[Countries.Length + MoreCountries.Length];
Countries.CopyTo(allCountries, 0);
MoreCountries.CopyTo(allCountries, Countries.Length);
mAdapter.NotifyDataSetChanged(allCountries);
Toast.MakeText(this, "New items were loaded. Now Click action is broken.", ToastLength.Short).Show();
}
}
1) issue is not a bug. If you want to use styles for you footer view you can create xml layout for it and then use
LayoutInflaterto instantiate it and add as footer view to your list.2) I also had problem with it and i have spent some time trying to find the problem. After all i decided to ignore it and use
NotifyDataSetChanged()method without parameters. And to update the adapter i have created method something like.SetItems(IEnumerable<TItem> items). So my code looks like this:And all works fine for me. ListItemClick delegate is invoking.