Check this link to understand AdapterView and Adapter.
This post is in continuation to below example.
Check this link to understand basic Example.
If we have only 1 View per Item, above example would suffice.
Incase if you have more than 1 View Item, example, 1 ImageView, 1 or 2 TextViews, then you have to follow below approach.
Below is the example that we will be working on.
Additional changes from the previous example are.
One New TextView to open a new screen. So, add Click Listener for it.
New List Item. So Define example list_item.xml (Same XML will be resued for both Screen 2 and Sceen 3)
(instead of android.R.layout.simple_list_item_1)
Our Data is not just String. So we need to define a new Data Model.
(intead of array of strings, We shall hold array of this DataModel object)
This is our custom Adapter.
ArrayAdapter that we have used earlier, holds only 1 Object.
In the previous example we have 1 item (i.e.,String). Now we have more items,
so we are defining a Class with required members, and we will use its Object (of new class).
height is hardcoded, only to tell its important to have fixed height value. else Adapter might show error while calculating the required height dp value for each list item.
** NOT RECOMMENDED
Problem with the above method is, if there are 1000 items to be displayed, there will
be 1000 views that will be created. But very few items can fit on Visible screen at a
time. So, the above procedure is memory intensive.
To make it efficient, we shall hold the views in a ViewHolder, and re-use the items,
fill it with new data as the user scrolls the screens.
Please Note, ViewHolder is only RECOMMENDED Practice in ListView. But it is Mandate