8. Adapter in Android
• An Adapter object acts as a bridge between an View and the
underlying data for that view.
• One side of the adapter is a data structure like a Java object
storing data. the other side of the adapter, there is view that
the data structure was transformed into.
• The Adapter is also responsible for making a View for each
item in the data set.
11. Adapter in Android
• Android ListView (con’t)
– Naive and dumb way
– The correct way: List13.java
– The fast way: List14.java
12. Adapter in Android
• Considering Adapter for the List View
– Each List item in the List View would be calling its
“getView()” method.
– This method would return a view for the List.
– But creating a new View for each list item would be
complicated, and the UI would be slow down.
– Instead of creating new View for each item, Recycler
save these view and passes it to the Adapter.
– Adapter uses these view sent by the recycler, when it
has to produce any new views.
13. Adapter in Android
• View Holders
– View Holders hold some data that related to an item to
the view.
– View Holders minimizes the amount of work, while
writing the same piece of code again and again.
– These are used while creating the views, where we have
to use more than one time.
(TextView)findViewById(R.Id.text);
(ImageView)findViewById(R.Id.Icon);
14. Adapter in Android
• View Holders (con’t)
– By using the View Holders, the views are saved into a
class variable and can be called when needed.
holder.text = (TextView)findViewById(R.id.text);
holder.icon = (ImageView)findViewById(R.id.icon);
convertView.setTag(holder);
– A tag can hold any kind of object, that can be set on the
view.
17. Cache technique in Android
• Basic cache view technique (for
views that have scrolling
behavior)
• Cache data technique
18. Adapter in Android
• Basic cache view technique (for
views that have scrolling behavior)
– Demo EfficientListView
19. Adapter in Android
• Cache data technique
– Why using cache:
• Some operations are high CPU load and
time consuming. E.g.: load images from
network…
-> Never do something time consuming twice
• Mobile applications, especially Android
applications needs optimization for smooth
UI.
20. Adapter in Android
• Cache data technique
– Demo ImageDownloader with soft
(memory) cache