Caching Views

0 views
Skip to first unread message

Vince

unread,
Jan 3, 2010, 1:51:47 PM1/3/10
to cw-android
New to Android and have recently purchased a Warescription. I have
been reading The Busy Coder’s Guide to Advanced Android Development
and working through Android Programming Tutorials. In tutorial 5 one
of the Extra Credit problems was to use 3 different views for the
rows. So I created 3 views, one with one Icon, one with two Icons and
one with three Icons. Changed the Radio group to Check Boxes which now
enables a Restaurant to be of multiple types. Everything works fine
but I notice through debugging that more often then not that the
convertView was not what was needed and a view had to be inflated. So
is there any way to cache the views with the icons already loaded
(there would be 7 different combinations possible with the 3 icons)
this way if the view had previously been used it would be cached and
no inflation or icon assignment would be required,only name and
address change required. Obviously it would be extra overhead for the
storage of the views but I was just wondering if the concept is
feasible.

Vince

Mark Murphy

unread,
Jan 3, 2010, 2:26:06 PM1/3/10
to cw-an...@googlegroups.com

I do not completely follow the question. If by "the convertView was not
what was needed and a view had to be inflated", you mean that you have
several different layout files that form your rows, you will also need to
implement/override the following methods in your adapter:

getViewTypeCount()
getItemViewType()

Then, Android will only hand you a proper view in getView().

However, you should not need to inflate different layouts just to get
different icons. Just use setImageResource() and setVisibility(View.GONE)
as needed to change or hide icons you do not need.

--
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html


Vince

unread,
Jan 4, 2010, 1:23:47 AM1/4/10
to cw-android
Mark,

I do have three different layout files and I do understand that I can
accomplish this with one layout just by changing the visibility of the
icons, but for the sake of understanding the use of using multiple
layouts for a row I thought it would suffice. I am assuming that
getItemViewType is called whenever the list needs updating. It’s
passed the current position of the list and returns the type of view
we want. What does the type value that gets returned actual reference
and how does getView actual use it. I need to understand this before I
can override it, I have been looking for a simple example but have not
come across anything I can make heads or tails of.

Vince

> Mark Murphy (a Commons Guy)http://commonsware.com
> Android App Developer Books:http://commonsware.com/books.html- Hide quoted text -
>
> - Show quoted text -

Mark Murphy

unread,
Jan 4, 2010, 7:01:58 AM1/4/10
to cw-an...@googlegroups.com
> I am assuming that
> getItemViewType is called whenever the list needs updating.

Not exactly. It is called before getView(), to determine what sort of row
will be needed for/returned by getView().

> What does the type value that gets returned actual reference
> and how does getView actual use it.

getView() doesn't use it directly. The type is a unique integer indicating
which type of row is being used by the given position. These integers are
used to identify discrete object pools for each type of row, so the
recycling works.

> I need to understand this before I
> can override it, I have been looking for a simple example but have not
> come across anything I can make heads or tails of.

This is covered in _The Busy Coder's Guide to Advanced Android
Development_, in the "More Fun with ListViews" chapter. There is a sample
application showing a list with two types of rows: normal and header.

Reply all
Reply to author
Forward
0 new messages