<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ibanga Enoobong Ime</title>
    <description>The latest articles on DEV Community by Ibanga Enoobong Ime (@ienoobong).</description>
    <link>https://dev.to/ienoobong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30335%2Fd8e3ce58-5b13-4930-92f4-176a6992e69b.jpeg</url>
      <title>DEV Community: Ibanga Enoobong Ime</title>
      <link>https://dev.to/ienoobong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ienoobong"/>
    <language>en</language>
    <item>
      <title>Creating custom array adapters in Android, A better way.</title>
      <dc:creator>Ibanga Enoobong Ime</dc:creator>
      <pubDate>Wed, 20 Dec 2017 13:34:20 +0000</pubDate>
      <link>https://dev.to/ienoobong/creating-custom-array-adapters-in-android-a-better-way-39c5</link>
      <guid>https://dev.to/ienoobong/creating-custom-array-adapters-in-android-a-better-way-39c5</guid>
      <description>&lt;p&gt;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 &lt;a href="https://developer.android.com/reference/android/widget/ArrayAdapter.html"&gt;ArrayAdapter&lt;/a&gt; class for each type I had&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;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 😞.&lt;/p&gt;

&lt;p&gt;While the awesome &lt;a href="https://twitter.com/kingsleyadio"&gt;Kingsley Adio&lt;/a&gt; was reviewing my code, he was quick to point out that I shouldn’t use the &lt;strong&gt;toString&lt;/strong&gt; 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 &lt;strong&gt;toString&lt;/strong&gt; says that the returned string should be “a concise but informative representation that is easy for a person to read”&lt;/p&gt;

&lt;h2&gt;
  
  
  When practical, the &lt;strong&gt;toString&lt;/strong&gt; method should return all of the interesting information contained in the object — Effective Java
&lt;/h2&gt;

&lt;p&gt;Returning just the desired name doesn’t follow this advice, also as the &lt;strong&gt;toString&lt;/strong&gt; method is very useful in debugging it’s wrong to just twist it to suit your selfish needs.&lt;br&gt;
Another thing to consider is accidental success. Say you forgot to override &lt;strong&gt;toString&lt;/strong&gt; appropriately, you’ll end up seeing stuff like &lt;strong&gt;co.package.name.Model@12f114&lt;/strong&gt; on the UI, or the bogus &lt;strong&gt;toString&lt;/strong&gt; that comes by default if you use data classes&lt;br&gt;
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&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Things look a whole lot cleaner now!&lt;br&gt;
Learnt something new from this post? Share! and star the &lt;a href="https://github.com/IEnoobong/CustomArrayAdapter"&gt;repo.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ve been in a similar situation? Let me know how you went about it, send me a DM on &lt;a href="https://twitter.com/IEnoobong"&gt;Twitter&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Reference:&lt;br&gt;
&lt;a href="https://www.javaworld.com/article/2073619/core-java/java-tostring---considerations.html"&gt;https://www.javaworld.com/article/2073619/core-java/java-tostring---considerations.html&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Originally appeared &lt;a href="https://medium.com/@IEnoobong/creating-custom-array-adapters-in-android-a-better-way-dc492c89512"&gt;here&lt;/a&gt;
&lt;/h3&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
