So I know there are a lot of steps in that quiz and it was a lot to process.
So let's start off by looking at the Android flavor app and
understanding that code base first because it has
all these pieces working together in a single project.
Remember, you can always download a simple version of the project, import
app, and enter studio and it be easier to navigate through all these files.
So if we scroll down to the bottom,
we see the ReadMe which is the explanation of what this app is.
So this example uses a list view and
a custom array adapter called Flavor Adapter to display a custom list
item view that contains that image and two text views.
And so all this information is used to display the Android flavor names and
version names.
So this item layout pattern and
custom adapter sound very similar to what we need for the Miwok app.
So as a note, whenever the comments in the app refer to the adapter view
it means a view such as the list view or grid view.
Which are common adapter views.
So here we are again in Android Studio.
So I downloaded a zipped version of the project and
opened it here in Android Studio to explore it some more.
So to see how this project looks, let's first explore the Java files.
So opening up, we see that there are three Java files here.
And the first one to take a look at is the MainActivity.
So given its name, it's probably the main screen of the app.
So when we first open the app the main activity is created by the onCreate
method and then we set the content view to be activity_mainlayout.
So following that, we see there is an array list call Android flavors that
contains objects of the type Android flavor.
So Andrew flavor doesn't sound like a standard class.
So it's most likely a custom class.
Now to check this out, we'll use the declaration function in Android.
So to use this, iIll right click on the name of the class,
go to Go To and then Declaration.
Nice so notice the jumps us to the Android flavor class.
So the AndroidFlavor.java is a custom class that we created.
Notice here that the objects of this class have three properties,
version name version number, and image resource ID.
So values for these are signed in the constructor which we find down
here and this class also contains some getter methods.
So what we've done is we've created an array
containing these AndroidFlavor objects.
So here we have an example of a single AndroidFlavor object.
So, with the knowledge of the box and the states and sign, and
we've taken each of these objects and put them inside an AndroidFlavors array.
So, many AndroidFlavor objects are stored in an androidFlavors array.
So, let's go back to the MainActivity.
So, we just saw the array list getting created up here.
So next are the array adapter calls.
So here's the line where we instantiate a custom adapter that
is stored in a variable called flavorAdapter.
Now to create this AndroidFlavorAdapter,
we call the constructor and pass in two inputs.
So this one likely refers to the context and the second one AndroidFlavors.
It sounds like we're passing in our AndroidFlavors array so let's see more
of a host adapters to find by going to where AndroidFlavorAdapters to find.
So right click, go to Declaration.
So here's the AndroidFlavorAdapter Java file.
So notice at the top, AndroidFlavorAdapter extends
ArrayAdapter, meaning that it inherits from the ArrayAdapter.
So ArrayAdapter is a superclass and AndroidFlavorAdapterr is a subclass.
So in addition to just inheriting from ArrayAdapter, we also customize our
adapter class to expect the data source to be a list of AndroidFlavor objects.
So let's take a look at its constructor.
Notice here that we called the superclasses constructor, so
we're calling ArrayAdapters constructor here.
And pass in three inputs.
Now if you remember back to the discussion about inheritance,
because AndroidFlavorAdapter extends ArrayAdapter.
We can use the constructor from ArrayAdapter so
that we get all the logic to initialize the state of the ArrayAdapter for
free without needing to write that ourselves.
So this means we can take a look into ArrayAdapter documentation
to find out more about its constructor.
So I'm back here in the ArrayAdapter developer documentation.
I'm going to scroll down to the public constructor section.
Now out of all of these public constructors, the second to last one is
the one that we're using for our AndroidFlavorAdapter.
So what we call the supercross class constructor, AKA the array
adapters contractor, we need to pass in three inputs.
So the context, the list item layout resource ID, and
a list of AndroidFlavor objects.
So I want to copy this code and paste it over here so
that we can have it to refer to when we look back at our code.
Now we're back in Android studio looking at the code for
the AndroidFlavorAdapter and here's the constructor code for
the array adapter the superclass in this case.
So notice here that we pass in zero as the layout resource ID
because we don't need to rely on the superclass array adapter inflating or
creating a list item view for us.
Instead, our Get view method will mentally handle the inflating of
the layout from the layout resource ID ourselves.
So moving down a little bit further,
we'll see more about what I was talking about.
So, down here, we have the getView method, and we're overriding the getView
method from the superclass and adding in our own logic for a specific use case.
So, the comment above mentions that our getView provides a list item view for
an AdapterView of some sorts.
An AdapterView like we mentioned before could be either a ListView or
a GridView or some other type of AdapterView.
So the getView method should take in three parameters.
So the first is the position which is the position in the list
of data this layout should represent, the convert view
which is the RecycleView that needs to be populated and the parent ViewGroup.
So the list items will be added to this parent ViewGroup as children.
So the first step inside the GetView method is the getItem method
which is originally defined in the superclass ArrayAdapter.
So this method returns the item in the list at the given index position and
note that the return value of this method is an object with the datatype
AndroidFlavor.
And this is because in the class declaration,
we specified an ArrayAdapter will expect a list of AndroidFlavor objects.
So as a list a few calls the getView method for
different positions in the list.
The adapter will go get the items at the different positions,
in the list of androidFlavor objects.
So this androidFlavor object contains the information though populate the list
item and will read out that data from it shortly.
So the second input parameter of the getView method is convertView,
which we said earlier is an existing view that we can reuse.
However sometimes this view will be equal to null.
Meaning that there is no view that we can reuse.
So the view being null is common when you first open up the activity, and
you're creating items in the list for the first time to fill up the screen.
Now once the screen is filled up,
then it's likely that there will be a valid view that we can we reuse.
Now in our case,
because we know that we're working with list items, I'll assign convertView
to listItemView to make it clear what type of view we're working with.
First, we check there's an existing view that we can reuse
using this if statement.
If the list item view is dull,
then we execute this line of code inside the if statement and this inflates all
new list item view from the XML layout file called list item.
And in case you're wondering, we're using a LayoutInflater which turns
the XML layout file into actual view objects.
So we're manually inflating the view which is why we don't need to pass
the layout resource ID to the superclass in the constructor,
because remember here we passed in zero.
So once we know the listItemView was valid and
not null, then we can start update the text views and
image views within the list item based on the AndroidFlavor object.
So notice that we're using the fine view by ID calls to find the view within
the ID list_item_icon or list_item_name or list_item_number.
So once we find ImageView and TextViews, we can call methods like
setImageResource or setText and pass in the value from the AndroidFlavor
object to change the data displaying displayed on these views.
So to summarize, from the current Android flavor object,
we can get the different states from it such as the version name,
the version number, and image resource ID.
So knowing that information from the specific AndroidFlavor object,
we can set either the image of or the text view,
to be the information that we've gotten from the AndroidFlavor object.
So at the very end, we return a list item view.
Which is the list item view with updated information
from the current Android flavor object.
So this ListItemView will be added as a child to the AdapterView.
All right so that's an overview of how the AndroidFlavorAdapter
is a custom adapter the takes as its input a list of AndroidFlavor objects.
So, when a list of items uses requested,
the AndroidFlavorAdapter will find the Android view at the correct position and
then create or reuse a list item layout.
So the views will be updated based on the information in the AndroidFlavor
object and then the list item view is returned to the list view.
So back to the main activity.
So the last two lines are related to implementing the list view and
array adapter.
So one that we've seen before when modifying them you walk up.
So here we get to reference the listView and
we're calling that reference listView.
And then finally we attach the flavorAdapter to the listView so
we can populate it with data.
All right so we just unpacked a rather complex structure.
So let's recap all of it.
So first off we created a way to store custom objects.
And in this case we use the androidFlavors array
which contains AndroidFlavor objects.
Next we create a custom adapter that extends from the ArrayAdapter and
takes a custom object.
So in this case,
we created AndroidFlavorAdapter which extends from the ArrayAdapter.
And it took in the custom object AndroidFlavor.
Also for the last step, we modified the getView method
in the custom adapter to provide a list item view for your ListView.
So we modify the getView method to get AndroidFlavor objects
from the ArrayAdapter.
So those at the appropriate position and then to use that information to populate
either recycled View or to populate a newly created View.
So I highly recommend you to walk through this project on your own and
see if you can move through how all these pieces fit together.
So it's well documented.
If you do ever get lost, refer back to the walkthrough above.
If it helps, draw out a diagram of the connections.
So understand that the custom ArrayAdapter pattern is a very powerful
concept for displaying list in Android.