Android
Lists, adapters and recycling
Bibliography
1. Wallace Jackson, Android Apps for Absolute
Beginners, Apress, 2017
2. Hello Views,
http://developer.android.com/guide/tutorials/vi
ews/index.html
3. Android Application Fundamentals,
http://developer.android.com/guide/topics/fun
damentals.html
2
Contents
• MVC
• Lists
– Model
– Adaptor
• List Elements
– Simple
– from XML
• ListActvity
• Optimization
– Recycling
– Tags
– Cache
• GridView
3
Model View Controller
• Pattern
• Splits
– UI
– Program
– Data storage
• Advantages
– Programing with components
– Easier to maintain
• Disadvantages
– May be slow
4
MVC Empirical
Database (Model)
Display (View) Program logic (Controller)
5
MVC Schematics
6
Specific Activities
• ListActivity
– Specially for a list
– May have other components
– List ID
• android:id=“@android:id/list”
• Special functions
– onListItemClick (…)
– setListAdapter (…)
7
Adapter
8
ListView
• A vertical container
– Similar with ScrollView
– Optimized for movement
• Identical components
– Each row
– Almost true ;)
• Adapter’s role
– Creates these components
• Events
– setOnItemClickListener (…)
– setOnItemLongClickListener (…)
9
Adapter
• Part of View
• Specifies the way of
display
– Adapts the data
– Modifies the View
10
ListView Adapter
• Predefined
– ArrayAdapter
• User defined
– Implementation of ListAdapter
– Extend BaseAdapter
11
Usage of ArrayAdapter (XML)
• Example
– Static list
12
Usage of ArrayAdapter (Java)
• Example
– Static List
13
Usage of ArrayAdapter (XML)
• Example
– Dynamic List
14
Usage of ArrayAdapter (Java)
15
A simple list
• ListView
– Contains a list
– android:id=“@android:id/list”
• Elements of the list
– android.R.layout.simple_list_item_1
• Static
– May use String[]
• Dynamic
– Use ArrayList<String>
16
The List with complex elements
• Elements
– Components
• EditView
• ImageView
• Container
• Adapter custom-made
– Implement ListAdapter
– Extend BaseAdapter
17
Extend BaseAdapter
• View getView (int position, View convertView,
ViewGroup list)
• int getCount ()
• Object getItem (int position)
• long getItemId (int position)
• int getViewTypeCount ()
• int getItemViewType (int position)
18
Extend BaseAdapter (XML)
19
Extend BaseAdapter (Java)
20
Extend BaseAdapter (Java)
21
Extend BaseAdapter (Java)
22
Extend BaseAdapter (Java)
23
Recycling
• Objects reusage
• Avoid object creation
– new is bad for memory!
• Use
– Buffers
– Lists
24
Optimisation
Inefficient!!!
25
Optimisation
Recycling
26
Different types of elements?
int getViewTypeCount ()
int getItemViewType (int position)
27
One more problem
Slow!!!
28
Tags (for View)
• void setTag (Object tag)
• Object getTag ()
29
Efficient using Tag
Utilizarea tag-ului
30
GridView
• Identical with ListView
– Extend BaseAdapater
31
The new RecyclerView
• A new way of building lists
– Uses several layouts
• Not just a LinearLayout
– The developer has to use recycling and tagging
• Extend RecyclerView.Adapter
– onCreateViewHolder()
• Inflate the view
– onBindViewHolder()
• Set the View’s data
– getItemCount()
32
Conclusions
• Lists
– Linear containers
– Contain components
• Including containers
• ListActivity
– Activity with special functions for list
• Adapter
– Formats the data from the model for the list
– Simple
– Custom made
– Recycling of elements
• Elements
– Described in XML
33
Keywords
• MVC
• Adaptor
• Classes
– ListActivity
– ListView
– GridView
– View
– ArrayAdapter
– BaseAdapter
– ArrayList
• Interfaces
– ListAdapter
• Recycling
• Tags
34
Questions
35

MDAD 4 - Lists, adapters and recycling