Creating custom array adapters in Android, A better way.
Tags: android, kotlin, softwaredevelopment
Published: 2017-12-20 13:34:20

---
title: Creating custom array adapters in Android, A better way.
published: true
description: Creating custom array adapters in Android, A better way.
tags: Android, Kotlin, Software Development
cover_image: https://thepracticaldev.s3.amazonaws.com/i/qhihvlj7qoc4s0g0zllr.png
---

I worked on a project recently and I had tons of spinners to be created, with data populated from a network resource. I first of all began with extending the [ArrayAdapter](https://developer.android.com/reference/android/widget/ArrayAdapter.html) class for each type I had

{% gist https://gist.github.com/IEnoobong/52456df2737d294feb779a050da1fb74 %}

After writing basically the same classes for 2 times, I figured that it didn’t make sense and there must be a better way to do it, after some googling, I discovered I could do without extending ArrayAdapter only problem was that I’d have to override the toString method of the classes with the desired display name, since the default array adapter implementation uses that 😞.


While the awesome [Kingsley Adio](https://twitter.com/kingsleyadio) was reviewing my code, he was quick to point out that I shouldn’t use the __toString__ implementation for UI elements. This lead me to investigate why, I discovered a good answer and that reignited a reason for me to start reading Joshua Bloch’s Effective Java. I’ve had the book for years and heard good things about it but never really got to read it 😁(I’m in Item 27 now! 💪 I also hear the 3rd edition is ready, sigh 😩). The general contract for __toString__ says that the returned string should be “a concise but informative representation that is easy for a person to read”

##When practical, the __toString__ method should return all of the interesting information contained in the object — Effective Java

Returning just the desired name doesn’t follow this advice, also as the __toString__ method is very useful in debugging it’s wrong to just twist it to suit your selfish needs.
Another thing to consider is accidental success. Say you forgot to override __toString__ appropriately, you’ll end up seeing stuff like **co.package.name.Model@12f114** on the UI, or the bogus **toString** that comes by default if you use data classes
After some back and forth with him 😍, he suggested that I create an interface with a display name variable which classes that implement will override to set the UI name, then use my custom array adapter repeatedly

{% gist https://gist.github.com/IEnoobong/fdeae71c4f2b2ce39f27d0594756e49b %}


{% gist https://gist.github.com/IEnoobong/b5b097ad21a31db604284ddbfc50e0e4 %}

Things look a whole lot cleaner now!
Learnt something new from this post? Share! and star the [repo.](https://github.com/IEnoobong/CustomArrayAdapter)


You’ve been in a similar situation? Let me know how you went about it, send me a DM on [Twitter](https://twitter.com/IEnoobong)!

Reference:
https://www.javaworld.com/article/2073619/core-java/java-tostring---considerations.html

###Originally appeared [here](https://medium.com/@IEnoobong/creating-custom-array-adapters-in-android-a-better-way-dc492c89512)