Step by Step: Adding a ListView
Ingredients (variables)
These can be member variables or declared at run time inside whatever method you so choose.
- ArrayList
- ListView
- ArrayAdapter
- OnItemClickListener
Recipe
- Declare our variables. These are defined in the ingredients list above.
- Find your ListView using
findViewById()
and assign it to your ListView - Populate (or add) data to your ArrayList
(this may require instantiation). - Add a
list_view_row
layout. The root element must be a TextView. Feel free to customize this as you want for now. - instantiate our new ArrayAdapter. The first argument is our context (
this
orMainActivity.this
), the second is our list_view_row layout (R.layout.your_row_name
); the final argument is our ArrayList (mArrayList
). This binds our List of data to our ListView. Super, big, important deal. Thanks, ArrayAdapter - you da real MVP. - instantiate our OnItemClickListener and make sure it is declared as a child of
AdapterView
or things may not work as intended. - Override the
onItemClick
method inside of your OnItemClickListener to do things. - Attach our OnItemClickListener to our ListView to bind everything together. This uses the ListView.
setAdapter()
method with your ArrayAdapter as an argument. - Done. :) Run. yay. party.
Challenge
- Follow the above instructions to create a new Android app for your favourite food.
- Gotta eat 'em all.