ListView in Android – Implementation and Overview

ListView in Android – Implementation and Overview

ListView in android is used to display list of items grouped together. The items are scrollable and set using the adapter (We will see about it a bit later). Think of list of items you want to see in your android application like this.

ListView Sample
ListView sample

You are going to use the XML Tag ListView to initialise the list of items.

<ListView android:layout_width=”wrap_content” android:layout_height=”wrap_content”> </ListView>

Our sample Layout for the project

 

Layout file for our app
Layout file for our app

This will initialise a simple dummy ListView. But what do we do if we need to add content to it.

ListView – How do you insert Content?

If you notice the image above, we see two textviews in that. What are they?

The Item and sub item text. So it is clear that, we can hold textviews inside a ListView. How do we achieve this?

You can do this by creating a layout which holds a textview inside it like below.

Layout with TextView inside it
Layout with TextView inside it (list_view.xml)

The textview to be present inside the listview is given. But how do you attach the two together.

 

We look at something called as adapters. Adapters are a way to model the data we have to the way the list view will understand it. In other words, we can convert the list of data we have (Could be any data – values pulled from a database/firebase or from a server while communicating) and set it to the ListView. The adapter will take care of creating multiple textviews dynamically and setting the text value to it. We can concentrate on modelling the data alone.

Android presents with

Implementing Adapters in Our Project

We will be using the Array adapter for this tutorial. You can refer my other tutorials for clearer implementation of other types of adapters. All the adapters inherit the Base Adapter class. We can also inherit the class to create our custom adapter for our application.

Check the code snippet below for a implementation of Array adapter

ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.list_view,R.id.label1,stringArray);
  •  First Parameter – It is the context of the application (To know more about context check here)
  • Second Parameter – The layout we have created for holding our TextView
  • Third Parameter – The id of the TextView we need to populate
  • Fourth Parameter – The Array of values we need to populate in the ListView (The adapter takes care of converting the Values to toString())

 

Once you have created the Adapter, we can set it to our ListView like below.

ListView listView = (ListView) findViewById(R.id.listBody); //ListView we have created in our main.xml
listView.setAdapter(adapter); // calling the public parameter setAdapter() to set the values

The entire code structure for creating the activity is given below

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        String[] stringArray = {"Value1","Value2","Value3"};

        ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.list_view,R.id.label1,stringArray);
        ListView listView = (ListView) findViewById(R.id.list_item);
        listView.setAdapter(adapter);
}
}

My Output screen

Output Screen
Output Screen

 

Voila! We have created our List View successfully. Learn more about the list view’s in my upcoming tutorials.

There are lot of tricks involved to create the perfect list view and i will also be writing tutorials on Adapters as much as possible.

Drop in any comments about this tutorial below.

 

“Learn and be Curious”.

Leave a Comment