Working in Android with lists is a very useful tool. We can make menus for the user to choose from a list of items. We can also make lists to present data to the user. For simple lists in Android the ListView widget can support a list of identical data types. For instance, a list of strings can be supported with the ListView widget. However, for complex data types we need to make use of the ListActivity class. In the previous weeks I have shown how to get the location data from a device via the GPS sensor. I have also shown how to pass that data to a web service (i.e. the National Weather Service) and then parse the received XML document. In this example I will show how you can render the parsed information into a List and display it to the user.

  1. The first step in this example is to inherit the activity from the ListActivity class. This will allow us access to the methods that we need to implement in order to display the list. Therefore, at the beginning of your activity where you would normally see ActivityName : Activity, you will now have ActivityName : ListActivity
  2. Next we need to create the list adapter that we will use to bind the data to a list. In this example, remember we are binding the weather observations that we parsed from the XML document. However, this technique can be used with other complex data types such as a Customer class. The WeatherListAdapter inherits from the BaseAdapter class. We then set the context for the adapter to the current activity and the items to the List<WeatherObservation> items. The remaining methods are required for the BaseAdapter class. In this example we are not doing much with these. However, you could, for instance, write the code to display a Toast to the user when an item of the list is clicked and whole host of other uses.
  3. In the OnCreate procedure we then register the WeatherListAdapter that we just created as the ListAdapter for the current activity.
  4. Now we must set the view for our list items. In this example I have created a custom view as an XML document which is saved in the Resources/layout folder. This is just an XML document where I have set attributes for the row items.
  5. Next, I override the GetView procedure and this is where I link the list to my custom view. Notice that when I inflate the view I am calling my custom view with Resources.Layout.WeatherObsListItemRow that I created in the previous step. Now, in the next lines of code I am tying the items in my list to the custom TextView widgets that I created in my layout file. And viola, my list can now be populated and shown to the user using my custom layout.

I hope that these last few tutorials have been as fun for you to read as they have been for me to write. I truly enjoy working in the Android space, and I hope you come back for more helpful tips. Next week, I am going to dive into the topic of Fragments in Android and how we can use them to display a list of items and then the detail about those items in different ways. I hope you stop by next week to see that post. Remember to share this post on your favorite social media platform by clicking the button below. And as always…

Happy Programming!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s