lecture slides for list views [android ]
TRANSCRIPT
![Page 1: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/1.jpg)
ListView and Adapters
Software Development for Portable Devices BITS Pilani Goa Campus
Sem I 2011-12
![Page 2: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/2.jpg)
CS C314 Software Development For Portable Devices 2
Foundation of :
![Page 3: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/3.jpg)
3
Problem??• Performance
• 10,000 entries in a list!– Less than 10% shown but fetched
completely
![Page 4: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/4.jpg)
4
Solution??• Populate on Demand– Google image search
• Recycle views to reduce object churn
![Page 5: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/5.jpg)
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.
![Page 6: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/6.jpg)
6
The FLOW diagram
![Page 7: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/7.jpg)
7
Adapters•extends "BaseAdapter“
•"ArrayAdapter" can handle data based on Arrays
•"SimpleCursorAdapter" handle database related
data
•Your own "CustomAdapter"
![Page 8: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/8.jpg)
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
![Page 9: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/9.jpg)
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
![Page 10: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/10.jpg)
ListActivity with ArrayAdapter and Android standard layout
• See the code FirstExample
CS C314 Software Development For Portable Devices 10
![Page 11: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/11.jpg)
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
![Page 12: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/12.jpg)
ListActivity with flexible layout
• See the code ThirdExample• Override getView().• Performance is not optimized.. Think why?
CS C314 Software Development For Portable Devices 12
![Page 13: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/13.jpg)
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
![Page 14: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/14.jpg)
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
![Page 15: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/15.jpg)
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
![Page 16: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/16.jpg)
Optimized way
• See the code
CS C314 Software Development For Portable Devices 16
![Page 17: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/17.jpg)
Your task
• Your task is to implement the View holder pattern in the Add food class
CS C314 Software Development For Portable Devices 17
![Page 18: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/18.jpg)
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
![Page 19: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/19.jpg)
Try Out
• LongClickListeners• Single Vs Multiple selection• Header and Footers
CS C314 Software Development For Portable Devices 19
![Page 20: Lecture Slides for List Views [Android ]](https://reader033.vdocuments.us/reader033/viewer/2022061611/554f82efb4c905d25b8b4b15/html5/thumbnails/20.jpg)
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