Using Adapters with List View
In this example , we will be using Adapter to diaply data in a List. Also, whenever user clicks on a particular item, respective image shows up in the image view below.
Steps to develop this can be summarized below -
- Make a new project with Empty Activity.
- Add two arrays - one for names another for respective images
- Use ArrayAdapter to connect the data source i.e. array and view.
- Make a layout which contains a listview and an Image View.
- Make a testview to be sued to display individual items in the listview.
- Implement AdapterView.OnItemClickListener and override its method - OnItemClickListener
- Any click on one of the items on the list is detected by onItemClickListener
- OnItemClick method provides the position of the click,AdapterView object, clicked view id and view object
- We use the position argument to display the respective image on the ImageView.
Used Concepts & Used Version:
Now, let us begin the development...
1.ListDisplay Activity (Launcher Activity)
2. Layout for ListDisplay.class
3.activity_listview.xml (For every unit of list view)
Whenever user clicks on one of the list items, they see respective image below.