I am playing around with Monodroid, trying to set an adaptor to a listview at start up. I have a bunch of controls on the screen, what I am trying to do is when the button is clicked an item goes into the listview.
I don’t get any error from the debugger, just a “Unfortunately Application1 has stopped”, which makes it hard to work out what I am doing wrong.
Here is my code:
public class Activity1 : Activity
{
List<string> items = new List<string>();
ArrayAdapter<string> adaptor;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
int i = 0;
items.Add("Hello world");
adaptor = new ArrayAdapter<string>(this,
Resource.Id.listView1,
items);
Button button = FindViewById<Button>(Resource.Id.button1);
ListView listView = FindViewById<ListView>(Resource.Id.listView1);
listView.SetAdapter(adaptor);
button.Click += delegate
{
items.Add("Hello " + ++i);
adaptor.NotifyDataSetChanged();
};
}
}
Upon a comment I found the Device Log with the following:
android.content.res.Resources$NotFoundException: Resource ID #0x7f050003 type #0x12 is not valid
This is the ID for the listview in Resource.Designer.cs
public const int listView1 = 2131034115;
The listview is declared like this:
<ListView
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="wrap_content"
p1:layout_height="fill_parent"
p1:id="@+id/listView1"
p1:layout_marginBottom="70dp" />
I figured it out after reading through the documentation. When instantiating the ArrayAdapter it must be instantiated with reference to Android.Resource.Layout.SimpleListItem1 or another list item resource, rather than the listview itself.