Lecture Slides for List Views [Android ]

3,482 views

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,482
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
237
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • position is the clicked element's position in your Adapter (so you can doadapter.getItem(position) ) row id is the id that corresponds to that element, what your Adapter returns in the getItemId()method.
  • Lecture Slides for List Views [Android ]

    1. 1. ListView and Adapters Software Development for Portable Devices BITS Pilani Goa Campus Sem I 2011-12
    2. 2. CS C314 Software Development For Portable Devices Foundation of :
    3. 3. Problem?? <ul><li>Performance </li></ul><ul><li>10,000 entries in a list! </li></ul><ul><ul><li>Less than 10% shown but fetched completely </li></ul></ul>
    4. 4. Solution?? <ul><li>Populate on Demand </li></ul><ul><ul><li>Google image search </li></ul></ul><ul><li>Recycle views to reduce object churn </li></ul>
    5. 5. Android – ListView <ul><li>Its a view capable of displaying scrollable list of items </li></ul><ul><li>It only creates views (widget) if needed </li></ul><ul><li>It recycles views, e.g. if a row is not displayed anymore it will be recycled and only its content will change. </li></ul>
    6. 6. The FLOW diagram
    7. 7. Adapters <ul><li>extends &quot;BaseAdapter“ </li></ul><ul><li>&quot;ArrayAdapter&quot; can handle data based on Arrays </li></ul><ul><li>&quot;SimpleCursorAdapter&quot; handle database related data </li></ul><ul><li>Your own &quot;CustomAdapter&quot; </li></ul>
    8. 8. Android - ListActivity <ul><li>If Activity is primarily showing a list you should extend the activity &quot;ListActivity“ </li></ul><ul><li>It simplifies the handling of a &quot;ListView“ </li></ul><ul><li>Set Listadapter in the onCreate(), via the method setListAdapter(). </li></ul><ul><li>Register the click by onListItemClick(ListView l, View v, int position, long id) </li></ul>CS C314 Software Development For Portable Devices
    9. 9. onListItemClick <ul><li>Parameters </li></ul><ul><ul><li>l : The ListView where the click happened </li></ul></ul><ul><ul><li>v : The view that was clicked within the ListView </li></ul></ul><ul><ul><li>position : The position of the view in the list </li></ul></ul><ul><ul><li>id : The row id of the item that was clicked </li></ul></ul>CS C314 Software Development For Portable Devices
    10. 10.   ListActivity with ArrayAdapter and Android standard layout <ul><li>See the code FirstExample </li></ul>CS C314 Software Development For Portable Devices
    11. 11.   ListActivity with own layout <ul><li>See the code SecondExample </li></ul><ul><li>Define the layout in xml for each row </li></ul><ul><li>Define our own arrayAdapter and using Id telling adapter which UI element is the text </li></ul>CS C314 Software Development For Portable Devices
    12. 12.   ListActivity with flexible layout <ul><li>See the code ThirdExample </li></ul><ul><li>Override getView(). </li></ul><ul><li>Performance is not optimized.. Think why? </li></ul>CS C314 Software Development For Portable Devices
    13. 13.   Performance Issues <ul><li>Creating Java objects for each row is time and memory consumption. </li></ul><ul><li>findViewById() is a expensive operation </li></ul><ul><li>Android recycles rows (views) which are not displayed anymore. </li></ul>CS C314 Software Development For Portable Devices
    14. 14.   Optimized Way <ul><li>Using existing rows saves memory and CPU consumption . </li></ul><ul><li>convertView - The old view to reuse </li></ul><ul><li>avoid doing findViewById() if not necessary </li></ul><ul><li>ViewHolder pattern </li></ul><ul><li>175% faster then the older procedure </li></ul>CS C314 Software Development For Portable Devices
    15. 15.   ViewHolder <ul><li>The ViewHolder stores a reference to the required views in a row </li></ul><ul><li>ViewHolder is then attached to the row via the method setTag() </li></ul><ul><li>row is recycled we can get the ViewHolder via getTag() method </li></ul><ul><li>much faster then the repetitive call of findViewById() </li></ul>CS C314 Software Development For Portable Devices
    16. 16.   Optimized way <ul><li>See the code </li></ul>CS C314 Software Development For Portable Devices
    17. 17. Your task <ul><li>Your task is to implement the View holder pattern in the Add food class </li></ul>CS C314 Software Development For Portable Devices
    18. 18.   Interacting rows <ul><li>Your row layout can also contain views which interact with the underlying data model. </li></ul><ul><li>Example : Checkboxes </li></ul>CS C314 Software Development For Portable Devices
    19. 19. Try Out <ul><li>LongClickListeners </li></ul><ul><li>Single Vs Multiple selection </li></ul><ul><li>Header and Footers </li></ul>CS C314 Software Development For Portable Devices
    20. 20. References <ul><li>Spinner tutorial : http://developer.android.com/resources/tutorials/views/hello-spinner.html </li></ul><ul><li>ArrayAdapter: http://developer.android.com/resources/tutorials/views/hello-spinner.html </li></ul>CS C314 Software Development For Portable Devices

    ×