Lecture Slides for List Views [Android ]

Preview:

Citation preview

ListView and Adapters

Software Development for Portable Devices BITS Pilani Goa Campus

Sem I 2011-12

CS C314 Software Development For Portable Devices 2

Foundation of :

3

Problem??• Performance

• 10,000 entries in a list!– Less than 10% shown but fetched

completely

4

Solution??• Populate on Demand– Google image search

• Recycle views to reduce object churn

5

Android – ListView

• Its a view capable of displaying scrollable list of items

• It only creates views (widget) if needed

• It recycles views, e.g. if a row is not displayed anymore it will be recycled and only its content will change.

6

The FLOW diagram

7

Adapters•extends "BaseAdapter“

•"ArrayAdapter" can handle data based on Arrays

•"SimpleCursorAdapter" handle database related

data

•Your own "CustomAdapter"

Android - ListActivity• If Activity is primarily showing a list you should

extend the activity "ListActivity“• It simplifies the handling of a "ListView“• Set Listadapter in the onCreate(), via the

method setListAdapter().• Register the click by onListItemClick(ListView l,

View v, int position, long id)

CS C314 Software Development For Portable Devices 8

onListItemClick

• Parameters– l: The ListView where the click happened– v: The view that was clicked within the ListView– position: The position of the view in the list– id: The row id of the item that was clicked

CS C314 Software Development For Portable Devices 9

ListActivity with ArrayAdapter and Android standard layout

• See the code FirstExample

CS C314 Software Development For Portable Devices 10

ListActivity with own layout

• See the code SecondExample

• Define the layout in xml for each row

• Define our own arrayAdapter and using Id telling adapter which UI element is the text

CS C314 Software Development For Portable Devices 11

ListActivity with flexible layout

• See the code ThirdExample• Override getView().• Performance is not optimized.. Think why?

CS C314 Software Development For Portable Devices 12

Performance Issues

• Creating Java objects for each row is time and memory consumption.

• findViewById() is a expensive operation

• Android recycles rows (views) which are not displayed anymore.

CS C314 Software Development For Portable Devices 13

Optimized Way

• Using existing rows saves memory and CPU consumption .

• convertView - The old view to reuse• avoid doing findViewById() if not necessary • ViewHolder pattern

175% faster then the older procedure

CS C314 Software Development For Portable Devices 14

ViewHolder• The ViewHolder stores a reference to the

required views in a row• ViewHolder is then attached to the row via the

method setTag()• row is recycled we can get the ViewHolder via

getTag() methodmuch faster then the repetitive call of

findViewById()

CS C314 Software Development For Portable Devices 15

Optimized way

• See the code

CS C314 Software Development For Portable Devices 16

Your task

• Your task is to implement the View holder pattern in the Add food class

CS C314 Software Development For Portable Devices 17

Interacting rows

• Your row layout can also contain views which interact with the underlying data model.

• Example: Checkboxes

CS C314 Software Development For Portable Devices 18

Try Out

• LongClickListeners• Single Vs Multiple selection• Header and Footers

CS C314 Software Development For Portable Devices 19

References

• Spinner tutorial : http://developer.android.com/resources/tutorials/views/hello-spinner.html

• ArrayAdapter:http://developer.android.com/resources/tutorials/views/hello-spinner.html

CS C314 Software Development For Portable Devices 20

Recommended