Replace TextView with ListView
In this week’s session, the demo app that was created last week was modified to use the
ListView element instead of
ListView provides a nice UI for displaying the list of tasks together with a checkbox to indicate whether they were complete or not.
You can see the new UI of the application in the following screenshots.
ListView and ListAdapter
ListView is a control which can be used for creating list of scrollable items. The data to the ListView will be provided by
You can think of ListView as the “view” component in a MVC framework and ListAdapter as the “model”
The class which is going to act as the
ListAdapater should implement following methods
You can read more about ListView and ListAdapter from android documentation.
To add ListView to any activity, we have to include the
<ListView /> tag to the activity’s layout xml. In our sample app, the ListView tag is added to the main.xml file.
The Activity class that uses ListView should implement the
ListActivity instead of plain
Activity class. In our sample app, the
ViewTasksActivity class is derived from the
Android SDK provides some predefined ids which can be used some specific purposes. One such “magic id” is
We can assign this to any element that we want to be displayed when the
ListView is empty.
This week’s homework is to Build a simpler (but less flexible) way to load data into a list using an ArrayAdapter (instead of a BaseAdapter). I would be completing the homework and would be posting the explanation and the source code later this week. So stay tuned 🙂
I have uploaded source code for yesterday’s session in github and you can download it from there. I am also working in this week’s homework and will be posting the explanation and source code once I am done.
- Recovering Android KeyStore password
- Changing the playback speed of Youtube videos
- Using WebSockets with Android to control Arduino robots
- 10 more open source Android apps which every developer must look into
- Building Robots using Android and Arduino in Google Devfestx
- Transferring data from Android using Audio