Adapter and cache technique Trí Phạm – MultiUni - 2012
Jun 21, 2015
Adapter and cache technique
Trí Phạm – MultiUni - 2012
Adapter and cache technique
• Adapter in Android
• Cache technique in Android
ADAPTER IN ANDROID
Adapter in Android
• Adapter design pattern
Adapter in Android
• Adapter design pattern (con’t)
Adapter in Android
• Adapter design pattern (con’t)
Adapter in Android
• Adapter design pattern (con’t)
• 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.
Adapter in Android
Adapter in Android
Adapter in Android
• Android ListView
Adapter in Android
• Android ListView (con’t)
– Naive and dumb way
– The correct way: List13.java
– The fast way: List14.java
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.
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);
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.
Adapter in Android
• Android ListView
CACHE TECHNIQUE IN ANDROID
• Basic cache view technique (for views that have scrolling behavior)
• Cache data technique
Cache technique in Android
Adapter in Android
• Basic cache view technique (for views that have scrolling behavior)– Demo EfficientListView
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.
Adapter in Android
• Cache data technique
– Demo ImageDownloader with soft (memory) cache
Q & A