That’s No List, THIS Is a List! 52/100 Days…
You know that feeling when your college classes FINALLY help you in real life? I love when that happens.
I dipped into my object oriented programming lessons in Java to set up the ArrayAdapter used to display addresses in my app. You can find a video at the bottom of the page and a link to the source code here.
I created a class, Homes, which is used for creating objects containing each address. The Homes class contains three static Strings, each of which is an address used in this application. The objects are instantiated in the Search class and their addresses are used in an ArrayAdapter. The original adapter is copied to another dynamic adapter which is used for displaying the list of addresses. The dynamic adapter will later be updated depending on the zipcodes entered by users.
The original adapter is copied to another dynamic adapter which is used for displaying the list of addresses. The dynamic adapter will later be updated depending on the zipcodes entered by users. Currently, the list of addresses now appears under the EditText View in the Search Activity.
Home objects contain street numbers, street names, and zipcodes. At this time, three Home objects will be instantiated using each of the Static Strings. The original ArrayAdapter will be used to store the addresses used for each Home object and the dynamic ArrayAdapter will start as an exact copy.
Originally, I used an array of Strings to create the ArrayAdapter used for the ListView in the List Activity. My new approach uses a series of Home objects from which the addresses can be derived. This means I only need update the Objects in the adapter based on the zip codes entered by the user. The program now has the potential to check the zipcode attributes of each Object instead of dissecting a String for each element in the list.
Creating Homes objects using the static Strings is a simpler approach for this application. The constructor of the Homes class declares the address information when an object is instantiated. This divides the work between the Homes class, ListView, and the ArrayAdapter. The class creates the object and provides their information. This allows the ArrayAdapter to add/remove them depending on their zip code before updating the ListView. Additionally, the dynamic ArrayAdapter can be reset to it’s original state when users clear their input.
This update taught me how much I have left to learn about Android Development. I had to use some advanced Java programming concepts to finish this feature. Object Oriented Programming didn’t even cross my mind when I worked on the codingkata months ago. This made me give myself a sober look in the mirror. Yes, I want to teach developers about Android Development. But I still have a long way to go. Now is the part of my journey where I focus on building my skills.
Next, I’ll refactor the ListView in the View List activity by instantiating Home objects, same as the Search Activity. Afterwards, I’ll use the EditText view to obtain user input. Finally, I’ll add logic to the Search Activity which updates the list to display addresses matching user input.
For more on this, watch the YouTube video below. For the source code click here
Join the mailing list to see updates like this every week!
Coding Fanatic
Image by TeroVesalainen from Pixabay