Muss Mesmari
Muss Mesmari

Reputation: 133

Xamarin Android: how to display/Bind List in ListView?

I tried a few ways to bind List elements to ListView but it did not work out. I always get the error "can not convert List

List<string> numbersListString= new List<string> {
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
    };

listView numbersListView = root.FindViewById<ListView>(Resource.Id.numbers);


var adapter = new ArrayAdapter<string>(Context,
               Android.Resource.Layout.ActivityListItem, numbersListString);

adapter.AddAll(Android.Resource.Layout.ActivityListItem);
            numbersListView.Adapter = adapter;


Upvotes: 0

Views: 580

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

Cause: It seems that you used the value of params textViewResourceId as ActivityListItem in ArrayAdapter . However , it requires the source ID to be a TextView , not a string .

Solution: You can use SimpleExpandableListItem1 instead of it.

var adapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleExpandableListItem1, numbersListString);

For more details about List Item Layouts you can check https://robgibbens.com/androids-built-in-list-item-layouts/

Upvotes: 1

Related Questions