Learn Android — Lists and Adapters

The world’s most popular mobile OS — from phones and watches to cars and TVs.

Akshansh Dhing
Parsed Inc.
4 min readJun 15, 2018

--

Welcome to the Learn Android series. I’m going to share with you, why and how to start with Android and build your portfolio. I’ll share with you explanations and code snippets on how to implement the most important and basic things in Android.

In the previous article, we learned about the ArrayList data structures in Java used majorly in Android to hold data. In this article, we’ll learn about Lists and Adapters in general which are an integral part of any application.

Previous Article: Learn Android — Custom Data Holders

ArrayLists are flexible data holders which can increase or decrease in size with respect to the amount of content in them. But we know that ArrayLists only holds “object data types” compared to “primitive data types” and that we need to use the object grabbers to convert the primitive to object data types. We create a new ArrayList of Strings using —

ArrayList<String> myArrayList = new ArrayList<>();

To add and remove String elements from the ArrayList we use —

myArrayList.add("Hello");
myArrayList.add("World");
myArrayList.add("Welcome");
myArrayList.add("To");
myArrayList.add("Parsed Inc.");
myArrayList.remove("To");
myArrayList.remove(2);

To print the ArrayList or get the item at a particular position, we can use the ArrayList object’s get() method like —

myArrayList.get(0);for (int i = 0; i < myArrayList.size(); i++) {
System.out.println(myArrayList.get(i));
}

Let’s move on to creating a List of items. For this we’ll use something called as ListView which is an inbuilt view in Android and we’ll also learn about Adapters.

We have these ListView and Adapters because views need “Recycling”. The Android system has limited memory resource and it is not possible to create views for each of the item that is present. For example, if we look at the Twitter app, there are millions of tweets and if we create views for each of the tweet, our app is confirmed to crash because it’ll run out of memory.

That is why the views are recycled and only the views that are shown on the screen are present. There are spare views which are contained in a “bin” waiting to be reused. When we scroll out the list, the data from that view scrolled out is discarded and the view added to a “reuse queue”. While other spare views from the queue are “bind” to the data and added to the bottom of the list to give a sense of never-ending linear list.

Okay now let’s look at the implementation of ListView and Adapters

  1. First we will create the ListView XML item.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TargetActivity"
>

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>

</android.support.constraint.ConstraintLayout>

2. Then we’ll initialize and reference the item from the XML into the view.

package com.example.android.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

public class TargetActivity extends AppCompatActivity {

private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);

//Setting up the ListView from the XML.
listView = findViewById(R.id.list_view);
}
}

3. Now, we need to learn about the ArrayAdapter which is most common adapter used with the ListView. But what is an Adapter? It’s a bridge between the view components and the data. So, basically it is responsible for communication between the view and data by generating views and handling the incoming data. So without the Adapter, the ListView can be thought of as just an empty container.

So the Adapter holds the data and knows how to translate it into a ListView. The Adapter has all the information like number of items, the data list, the “instructions” to make a list item view, and more.

When the screen is full the ListView stops asking for more data from the Adapter which means the views are only created on demand when needed. And remember when the views are scrolled off, they are added to the “reuse queue” and called when there is a demand.

For a start, we’ll use the inbuilt ArrayAdapter and directly link it to the ListView. Here’s how we do it —

public class TargetActivity extends AppCompatActivity {

private ArrayList<String> myArrayList;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);

//Setting up the ListView from the XML.
listView = findViewById(R.id.list_view);
myArrayList = new ArrayList<>();
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myArrayList);
listView.setAdapter(arrayAdapter);
}
}

We’ve used the ArrayAdapter with Strings since we’ll be dealing with String data. Also, we’ve used the inbuilt XML resource “android.R.layout.simple_list_item_1” because we just need to display a few Strings.

Since we haven’t added any data, we’ll add it to the ArrayList using the myArrayList.add(); method. And then on running the app, we can see that we’ve a fully functional ListView that displays a set of Strings — recycling it when needed — in a linear fashion.

Next up: Learn Android — Custom Lists and Adapters

Stay tuned for regular updates. Follow me and Parsed Inc. to never miss another one!

Also, let’s become friends on LinkedIn, GitHub, Twitter and Facebook!

To learn more about me and my work, visit my website!

Follow ParsedInc. on Facebook, LinkedIn, and Instagram!

If you enjoyed this article, feel free to 👏👏👏 a few times and share with a friend to help it reach someone who needs to read it. Thanks!

--

--