DEV Community

loading...
Cover image for Creating custom array adapters in Android, A better way.

Creating custom array adapters in Android, A better way.

Ibanga Enoobong Ime
Java, Kotlin, Spring Boot Android Developer. Marathoner. #CU #KCOB
・2 min read

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 class for each type I had

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 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

Things look a whole lot cleaner now!
Learnt something new from this post? Share! and star the repo.

You’ve been in a similar situation? Let me know how you went about it, send me a DM on Twitter!

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

Originally appeared here

Discussion (0)