Download presentation
Presentation is loading. Please wait.
1
Android Lists and Fragments
2
Lecture Overview Introduction to Android lists (ListView)
Using a simple ArrayAdapter Handling list events Creating a custom adpater
3
<ListView> Introduction
The <ListView> widget displays a scrollable list of items Each item can be rendered using “default” layout or a custom layout Layouts are bound to data via an Adapter Note that grids work similarly The OnItemClickListener() callback fires when the user clicks an item The GridView works similarly
4
<ListView> Illustration
5
<ListView> Attributes
android:divider – color to draw between list items android:dividerHeight – to set the height of the divider android:footerDividersEnabled – Boolean to draw divider before footer row android:headerDividersEnabled – Boolean to draw divider after header row
6
<ListView> Methods (1)
Call setAdapter() to associate a ListView with an adapter Call setDivider() to draw color between list items Call setDividerHeight() to set the height of the divider drawn between list items
7
<ListView> Methods (2)
8
Introduction to Adapters
An adapter connects (binds) a ListView to a data source An ArrayAdapter binds to an array A SimpleCursorAdapter binds to a Cursor (dabase)
9
ArrayAdapter (Introduction)
Use this adapter to bind to an array By default, toString() is called on each array item and the contents placed into a TextView This behavior can be changed using custom adapters
10
ArrayAdapter (Constructor)
The ArrayAdapter constructor requires three arguments The application context A layout that contains a TextView A String array
11
ArrayAdapter (Constructor - Example)
The argument (this) references the context android.R.layout.simple_list_item_1 references a list item (TextView) from the global Android R file myStringArray contains the string array to be rendered <string> is the data type of the list items
12
ArrayAdapter (Associating)
Call setAdapter() passing the adapter as an argument Each array item is bound and rendered as an item in the list
13
ArrayAdapter (Handling Click Events)
The OnItemClickLIstenter interface and onItemClick event fires when the user clicks a list item To get the list item, call getItemAtPosition on the list view
14
ArrayAdapter (Handling Click Events)
listView.setOnItemClickListener(new OnItemClickListener() public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int itemPosition = position; String itemValue = (String) listView.getItemAtPosition(position); } });
15
Creating a Custom Adapter (Introduction)
It’s a multi-step process Create the class that will store each data time Weather in our example Crate a class that derives from ArrayAdapter Override the getView() event to get the View Create a class to store the object reference (Holder)
16
Creating the Data Class
Each list item will contain an icon reference an a string description
17
Creating the Custom Adapter
Create a class that derives from ArrayAdapter
18
Override GetView()
19
Configure the ListView
20
// 15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.