Xamarin – Display Entity Collection in ListView in Android
In this post, we would display a collection of some entity in ListView.
Let’s start with creating the following Person entity.
The Person entity class is located under Model.
Now let’s add the ListView template XML file under Layout folder.
Keep a TextView for now inside the XML file.
Add a ListView control to the Main Layout.
The following method would generate sample data.
Now in Activity1, we would get the ListView control and set the data by using following code.
Let’s run the application. Oops, the entity class name is displayed instead of data. That means we need to do a different way to display data.
Not so difficult, just create another class which would be derived from the abstract class BaseAdapter. The following properties and methods are implemented once you inherit the class.
The following modifications are done to the class, which is self-explanatory.
And back to Activity1, and do the following for displaying data in ListView.
Well, it has displayed with a single TextView inside the template.
How about displaying Email Id below the Full Name. This will require a change in the ListViewTemplate1 change. The following are the changes.
And in the Screen Adapter class, GetView would be changed like this.
Now run the application, and you would see the Email Id is also displayed.
Hope this post helps, thanks for reading.
Full Source Code: Click Here