Reputation: 543
I'm trying to create a listview in Xamarin Android, and run a foreach loop on a collection to populate the listview. However, i'm having problems just getting listview working properly.
I have:
ListView uuidListView = FindViewById<ListView> (Resource.Id.uuidList);
uuidListView.Adapter = new ArrayAdapter (this, Resource.Layout.UUIDlist, "some text");
The response i get when i run the program is:
The best overloaded method match for 'Android.Widget.ArrayAdapter.ArrayAdapter(Android.Content.Context, int, int)' has some invalid arguments.
Why is it looking for two integer arguments?
Please explain in as basic terminology as possible i'm fairly new. Thanks.
Once that is sorted out, i then need to run my foreach loop over my collection like this:
foreach (string uuid in uuids) {
if (uuid != "")
And populate the listview with all uuid values not equal to "". Any help on that would be great too.
Upvotes: 0
Views: 576
Reputation: 2453
Instead of passing the ArrayAdapter (this,Resource,"Some Text") pass it a list of items,
because you are passing three params it looks for the closest constructor to what you are passing it. There is no ArrayAdapter constructor that takes those three params so it loost at the context, int, int constructor as a suggestion.
Try using the constructor that takes a IList
public ArrayAdapter(Context context, int textViewResourceId);
protected ArrayAdapter(IntPtr javaReference, JniHandleOwnership transfer);
public ArrayAdapter(Context context, int resource, int textViewResourceId);
public ArrayAdapter(Context context, int textViewResourceId, Object[] objects);
public ArrayAdapter(Context context, int textViewResourceId, System.Collections.IList objects);
public ArrayAdapter(Context context, int resource, int textViewResourceId, Object[] objects);
public ArrayAdapter(Context context, int resource, int textViewResourceId, System.Collections.IList objects);
Upvotes: 1
Reputation: 5234
The first integer is the layout file, the second file is the text view inside the layout.
Change:
uuidListView.Adapter = new ArrayAdapter (
this,
Resource.Layout.UUIDlist,
"some text");
to
uuidListView.Adapter = new ArrayAdapter (
this,
Resource.Layout.UUIDlist,
Resource.Id.textView1);
where the textView1 is the ID of the text field on the layout.
Upvotes: 1