Click here to Skip to main content
15,886,519 members
Articles / Mobile Apps / Android

Display a String List in an Android ListView

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Jan 2015CPOL2 min read 14.7K   1  
How to 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, I have shown how to create the adapter and set it in the listview to display the list of items:

Java
private ListView mListView;

@Override
protected void onStart()
   {
   super.onStart();

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

   // Initialize 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()]));
  mListView.setAdapter(adapter);
  }

After the adapter for the list is set, you can also add an 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.

Java
private ListView mListView;

@Override
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() {

   @Override
   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());
      MainActivity.this.startActivity(projectIntent);
   }

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
Cindy Potvin is a software developer based in the Montreal area. At her day job, she creates web applications using the ASP.NET MVC framework and mobile applications using the Android SDK.

Comments and Discussions

 
-- There are no messages in this forum --