Learn Android — Custom Lists and Adapters

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

Akshansh Dhing
Parsed Inc.
7 min readJun 18, 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 explanations and code snippets on how to implement the most important and basic things in Android.

In the previous article, we learned about all the theoretical part of the workings about List and Adapters. But we used most of the inbuilt methods. So in this article, we’ll learn about creating and using Custom Lists and Custom Adapters.

Previous Article: Learn Android — Lists and Adapters

For our learning purpose, we’ll create a ListView row item similar to how contacts are displayed on our mobile phones. Let us do this step-wise.

Initially, you should create a ‘new’ project and go through all the fundamental details, including choosing the API level and upto choosing color schemes for your Android application. Please do choose an empty activity for learning purposes.

Next up, we’ll already have one XML and one Java file created in our project. Let’s see what we’ll do next to create perfectly, custom Adapters and Lists.

Using the already created activity_main.xml file, we’ll define the ListView in the XML like we did in the previous article. Also, we need to give an ID to the ListView so we can reference the view into the Java file.

This is how the layout will look after adding the ListView.

Now, we’ll create a new XML layout file. Let’s name it “list_item.xml” and then we can create a layout similar to how a single contact is displayed.

This will give us a horizontal list item with a contact image and 2 text’s for contact name and number.

The blueprint for the list item layout will look like this.

Furthermore, we’ll now need to define an object class. An object is a collection of variables. To create an object class, we create a new Java class. Let’s name it “ContactItem.java”.

We will then define 3 variables — for our 3 fields in the contact list item — and create constructor and getter & setter methods for the same. For the image, we will have an integer variable as for the “dummy data” that we’ll be including in the application will be of the integer data type.

If we access image through APIs, most of the time we get image URL’s, which are of the String form and we need to parse the URL’s to get the image.

We just need to declare the variable, the Android Studio IDE is capable of generating the constructors and methods for us. Let’s see how to do that. First of all, we’ll create the variables we need.

After this, press Alt + Insert on the keyboard and a drop down will let us choose constructor, getter, and setter methods and we’ll add them one by one. This reduces the amount of code that we need to type. While adding these, we’ll need to select all the variables.

Here’s the final Object class. This is also called as a POJO class or Plain Old Java Object.

After creating the POJO class, we’ll create the ArrayAdapter to link the data with the views. For more information about Adapters, please refer to the previous article.

But instead of our ArrayAdapters consisting of Strings like in the previous article, our “ContactAdapter” class will have ArrayAdapter consisting of our ContactItem class.

Next, the ArrayList that we will create will also contain ContactItem instead of the String since each element now contains the complete ContactItem details — including image, name, and number. Let us see how to implement the ArrayAdapter.

After creating the variables and the constructor, we will override some methods. The shortcut for browsing available methods that we can “override” is Ctrl + O.

The must-override methods while creating ArrayAdapters are —

  1. getView()
  2. getCount()

We will add these methods dynamically (Ctrl + O) so that we do not need to worry about the boilerplate but rather just the implementation of what we need to achieve. This is how our adapter will look after adding the methods —

In the getView() method, firstly we “inflate” the layout resource. Inflating a resource means we take the XML layout and parse it to create a hierarchy of views. This is used to render the layout when setContentView() is called.

After inflating the layout, we initialize the view components from the XML by using the findViewById() method. But since we’ve inflated the layout in the “view” object we use view.findViewById() and pass in the ID parameter.

After initializing the views, we get the item from the ArrayList using the “position” parameter and then set the data into the views using the getter methods that we generated in the object file. And then we return the view. Furthermore, in the getCount() method, we return the size of the ArrayList that contains the data.

Now, we need to instantiate all the required adapters and lists in the Java file of the activity we want to show the list into. Let’s see how we do that —

In our Activity class, we’ve initialized the list and the adapter along with our ListView. Then we populate the ArrayList with dummy data by replicating the same data a few number of times for the sake of observing the ListView. We use the .add() method and create a new ContactItem object and pass in the required parameters to add an item to the list.

You can add custom data to the list if you wish to observe it with a random data set.

Finally, we set the Adapter on the ListView and when we run the code, we’ll be able to see the ListView in action.

THAT’S IT! We’ve successfully implemented a custom data holding ListView and we are now able to create endless custom views.

Next up: Learn Android — RecyclerView

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!

--

--