Display a string list in an Android ListView

Showing a list of items is a very common pattern in mobile application. This pattern comes up often when I make a tutorial: I often need to interact with data, but I don’t want to spend a lot of time just on displaying that data when that’s not the point of the tutorial. So, what is the easiest way to display a simple list of values in Android like a list of strings?

In the Android SDK, the widget used to show lists of items is a ListView. A listview must always get its data from an adapter class. That adapter class manages the layout used to display each individual item, how it should behave and the data itself. All the other widgets that display multiple items in the Android SDK, like the spinner and the grid, also need an adapter.

When I was making the knitting row counter for my series on saving data with Android, I needed to show a list of all the projects in the database but I wanted to do the absolute minimum. The name of the projects are strings so I used the ArrayAdapter class from the Android SDK to display that list of strings. Here is show to create the adapter and set it in the listview to display the list of items:

private ListView mListView;

protected void onStart()

   // Add the project titles to display in a list for the listview adapter.
   List<String> listViewValues = new ArrayList<String>();
   for (Project currentProject : mProjects) {

   // Initialise a listview adapter with the project titles and use it 
   // in the listview to show the list of project.
   mListView = (ListView) findViewById(R.id.list);
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				               android.R.layout.simple_list_item_1, android.R.id.text1,
				               listViewValues.toArray(new String[listViewValues.size()]));

After the adapter for the list is set, you can also add a action to execute when an item is clicked. For the row counter application, clicking an item opens a new activity showing the details of the selected project.

private ListView mListView;

protected void onStart()

   // Sets a click listener to the elements of the listview so a
   // message can be shown for each project.
   mListView.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> parent, 
                           View view,
			   int position, 
                           long id) {
      // Get clicked project.
      Project project = mProjects.get(position);
      // Open the activity for the selected project.
      Intent projectIntent = new Intent(MainActivity.this, ProjectActivity.class);
      projectIntent.putExtra("project_id", project.getId());

If you need to go further than the default layout, you’ll need to create your custom layout and adapter to show the data the way you want it to, but what is shown here is enough to get started displaying data. If you want to run the example, you can find the complete RowCounter project on my GitHub at http://github.com/CindyPotvin/RowCounter: the listview is the MainActivity.java file.

Sign up now to get my newest articles about making applications and my ongoing side project delivered straight to your inbox.

Book Review : Soft Skills by John Sonmez

Most of my articles are about technical subjects, but being a developer is more than just typing code in a computer. Many of us were originally attracted to coding for the intellectual challenge, but to write code in real life you must also deal with the human factor. I had the chance to take a look at the early access version of the book Soft Skills: The software developer’s life manual by John Sonmez that is coming out today on Amazon. It does a good job to describe all those fuzzy parts of software development that we don’t always like to talk about.

The book starts off discussing the various career paths for a developer as an employee, as a freelancer or as an entrepreneur. It also goes on to explain how to market yourself as a software developer, which is my favorite part of the book. I started this blog last year to learn to communicate better, get out of my programmer’s cave and put myself out there more. John writes about going on even if it feels uncomfortable, which resonates a lot with me: I’ve always been shy, so I have to push myself before publishing something.

The rest of the book is more about having the right mindset and being in good shape physically, mentally and spiritually. I enjoy reading about productivity and learning and I was able to get a few new ideas. It also got me to try the Pomodoro technique again, which is great for days when you feel stuck. I mostly skipped over the end of the fitness section, since it is a bit overwhelming for me. This is one of my weak points: I eat well since I cook most of my food, but I’m just trying to get into the habit of doing a bit of yoga every day, along with gardening in the summer and walks in the winter. All the talk about muscle-building is a bit too much for now.

In conclusion, this book is the manual I wish I could give everyone that is starting out in software engineering. Even if you have a pretty good career already, the book covers such a large variety of subjects that you’re sure to get some interesting nugget of wisdom from it. It reminded me of many thing that I knew or thought I knew and could do better. If you want to improve yourself and your career in 2015, this is the book to do it!

Note: The links in this post are affiliate links, so I’ll get a small commission if you buy the book using them. You can also search for the book directly on Amazon.