<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Luca Del Rio on Medium]]></title>
        <description><![CDATA[Stories by Luca Del Rio on Medium]]></description>
        <link>https://medium.com/@luca.delrio?source=rss-43c9281a6a7e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4XpsJPiiXmlikSTUSi4yWg.jpeg</url>
            <title>Stories by Luca Del Rio on Medium</title>
            <link>https://medium.com/@luca.delrio?source=rss-43c9281a6a7e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:44:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@luca.delrio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ArrayAdapter Android: How to make your own ArrayAdapter]]></title>
            <link>https://medium.com/@luca.delrio/arrayadapter-android-how-to-make-your-own-arrayadapter-6572937c3a3d?source=rss-43c9281a6a7e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6572937c3a3d</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[custom]]></category>
            <category><![CDATA[autocomplete]]></category>
            <category><![CDATA[arrayadapter]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Luca Del Rio]]></dc:creator>
            <pubDate>Tue, 11 Oct 2022 08:39:37 GMT</pubDate>
            <atom:updated>2022-10-11T08:39:37.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A9EyscQgvR1qhgX8" /><figcaption>Photo by <a href="https://unsplash.com/@glenncarstenspeters?utm_source=medium&amp;utm_medium=referral">Glenn Carstens-Peters</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>First of all: what is an ArrayAdapter? An ArrayAdapter is a special type of Adapter used to show an array of data on the screen in a list or grid.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Oo5VZD2ICdgJOeC7.jpg" /></figure><blockquote>In this article we will address an issue encountered with the use of the AutoCompleteTextView component in conjunction with the ArrayAdapter component. The purpose is to intercept the user’s click on one of the list items and raise all the information inherent to the selected option. Getting the selected item is not straight forward, so we will create a custom <strong>AutoCompleteAdapter </strong>inheriting from ArrayAdapter. This article is organized in 2 sections, in the first I show you an approach using an ArrayAdapter where I am going to show you the problem of using a generic ArrayAdapter, while in the second I will show you how to create a custom adapter and get to the solution.<br>Let’s dive into this article!</blockquote><h4>The scenario we are presenting is as follows:</h4><ul><li>We have an array of objects of type Items that will be represented in a data class called ACAdapterCustomProjectUIModel, which contains three properties: id, name, image</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aeb183441de22dc168f7f5a20402ec4/href">https://medium.com/media/6aeb183441de22dc168f7f5a20402ec4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecaa551c70986c4b463cc9f009911ce7/href">https://medium.com/media/ecaa551c70986c4b463cc9f009911ce7/href</a></iframe><ul><li>We need this data to be populated in a layout with a ShapeableImageView and a MaterialAutoCompleteTextView in it</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ueUU0e0sK42MnJ-o3FQMA.png" /></figure><p>To design this UI I used a ConstraintLayout, and for the views inside it I am using Material Design components to have an app coherent with the operating system</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a382ff7bee3d7eb63368c4835dba4ae/href">https://medium.com/media/7a382ff7bee3d7eb63368c4835dba4ae/href</a></iframe><p><strong>I will use the MVVM pattern, we start by creating the ViewModel to acquire the data and develop the logic.</strong></p><p>Our ViewModel will contain a function that will acquire the data and populate the list of ACAdapterCustomProjectUIModel. We also create a function that will take care of retrieving the correct icon url and across the LiveData share it with the fragment. This function will cycle through the complete list in order to find the item with the same name as the one chosen by the user (a bit cumbersome as a method).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/839eaed572d2c83dd3111f54d1e19440/href">https://medium.com/media/839eaed572d2c83dd3111f54d1e19440/href</a></iframe><p>Once the viewmodel and layout are completed we implement the <strong>fragment.</strong> We will set the MaterialAutoCompleteTextView component with an ArrayAdapter initializing it with the context, a layout and the list of strings that correspond to the names of our items.</p><p>After doing that we need to know which item was selected so we use the setOnItemClickListener.</p><pre>setOnItemClickListener { parent, view, position, id -&gt;</pre><p>Here we realize the great limitation of applying this solution, the function lets us know the location of the clicked item relative to the filtered list, filtered by user input, and this does not allow us to correctly match icon and name. A “workaround” solution would be to capture the text from the AutoCompleteTextView and scan the list to find the correct item.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39a7ca08d247c45190706bd8dbd9d82b/href">https://medium.com/media/39a7ca08d247c45190706bd8dbd9d82b/href</a></iframe><p><strong>The ideal solution for me was to create a custom adapter that solves this problem …</strong></p><p>Our <strong>AutoCompleteAdapter</strong> extends ArrayAdapter and has the function of filtering the list based on the input request and getting the item selected by the user, so the adapter is composed as follows:</p><ul><li>getCount which returns the number of items shown to the user</li><li>getItem which will return the item based on the position</li><li>getItemId which returns the id of the item based on the position</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15385c9f86bd7338eea55d713cddae47/href">https://medium.com/media/15385c9f86bd7338eea55d713cddae47/href</a></iframe><ul><li>getView puts text into the view that will be shown to the user and is responsible for listening to the click event of an item, through the function of an interface it notifies the fragment of the item clicked by the user</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78579121d60dc594b3fd401cadaa3ad2/href">https://medium.com/media/78579121d60dc594b3fd401cadaa3ad2/href</a></iframe><ul><li>getFilter that reads the sequence of characters entered as input by the user and returns a filtered list of possible requested items</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdb87e9a818f5cf53f762bc86a4d2836/href">https://medium.com/media/bdb87e9a818f5cf53f762bc86a4d2836/href</a></iframe><p>Then we update the fragment using our new AutoCompleteAdapter</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06338b7557cd98213a91ad5558cadca2/href">https://medium.com/media/06338b7557cd98213a91ad5558cadca2/href</a></iframe><p>this makes the call to setOnItemClickListener useless so we can remove it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a956acf84427707f24cf518951ea3b7/href">https://medium.com/media/1a956acf84427707f24cf518951ea3b7/href</a></iframe><p>We now implement the onItemSelected method by extending our fragment with the <strong>ACAdapterCustomProjectListener</strong> interface to inherit its functions. In the viewmodel we are going to create the onPositionTypeSelected method which we will use in the onItemSelected method in our fragment and passing it the required parameter, the item.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c9b9c93838d5b15561719e3ef491bab/href">https://medium.com/media/8c9b9c93838d5b15561719e3ef491bab/href</a></iframe><p>In the viewmodel we can finally replace the setIcon method with the new onPositionTypeSelected method to set the image and text using live data</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e946f1febc7805c3d6cc96f2479cd62/href">https://medium.com/media/6e946f1febc7805c3d6cc96f2479cd62/href</a></iframe><p>This is all is needed to create a custom ArrayAdapter!</p><p>You can find the complete <a href="https://github.com/DelRioLuca/ACAdapterCustomProject">project</a> in <a href="https://github.com/DelRioLuca">my GitHub</a></p><p>Created by <a href="https://medium.com/u/43c9281a6a7e">Luca Del Rio</a> Android Developer at <a href="https://www.overapp.com/">OverApp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6572937c3a3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>