ViewHolder paradigm vs. using View.setTag()

by Eric » Sun, 01 May 2011 12:27:59 GMT


Sponsored Links
 In the Google I/O 2010 - The world of ListView talk shown here:

 http://www.youtube.com/watch?v=wDBM6wVEO70& ;t=17m38s

a 'ViewHolder' paradigm is suggested to cache views and avoid the
findViewById lookup.  Could the same thing be achieved by using the
setTag(id, Object) method on View, and simply use the View's tag map
to store the reference by resource ID?  I think this would work, but
would it introduce any memory leak (or other) problems?


    public static final View getCachedViewByTagId(View parentView, int
viewToFindId) {
        Object object = parentView.getTag(viewToFindId);

        if(object == null) {
            object = parentView.findViewById(viewToFindId);

            if(object != null) {
                parentView.setTag(viewToFindId, object);
            }
        }

        return (View)object;
    }

-- 



Re: ViewHolder paradigm vs. using View.setTag()

by Romain Guy » Sun, 01 May 2011 12:48:32 GMT


 You could, but setTag(int, Object) forces you to use unique ids throughout
your app.







-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them

-- 


Sponsored Links


Re: ViewHolder paradigm vs. using View.setTag()

by Mark Murphy » Sun, 01 May 2011 22:49:50 GMT


 


Well, now, that's a remarkably gentle response.

I seem to recall you being a bit more... forceful, shall we say, when
I suggested using the two-parameter setTag() to avoid having to fuss
with a holder class. Something about it wasn't there for app
developers but rather for libraries, or some such. As a result, I
destroyed that section of the book and salted the earth it sprang
from. :-)

Are the indexed getTag()/setTag() save for app developer consumption?
And, if so, what rules should we be following?

Thanks!

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 3.4 Available!

-- 



Re: ViewHolder paradigm vs. using View.setTag()

by Eric » Mon, 02 May 2011 00:45:19 GMT


 


If you use the resource ID defined in the layout, that is always
unique by default, so that should work...?

-- 



Re: Re: ViewHolder paradigm vs. using View.setTag()

by Kostya Vasilyev » Mon, 02 May 2011 03:18:55 GMT


 01.05.2011 20:45, Eric :
If you use the resource ID defined in the layout, that is always unique by default, so that should work...?
It should, as using R.id.* values. However, if your item layout is relatively simple, I don't expect you'll get much of a speedup with a view holder vs. using itemLayoutRoot.findViewById. Have you done any profiling of your application? Anything interesting? -- Kostya Vasilyev -- http://kmansoft.wordpress.com --



Re: Re: ViewHolder paradigm vs. using View.setTag()

by Romain Guy » Mon, 02 May 2011 08:24:26 GMT


 >



It will depend on your phone. But even on a NexusOne and using list items 1
or 2 levels deep, the gain is around 4 or 5 fps. It's worth it.

-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them

-- 



Other Threads

1. Change Text color and text size in listview

Hi Prerna

1. Create your own adapter
2. use Html.fromHtml in getView method of adapter class





>> 

2. about XMPP

Dear mates

I am trying to build a small XMPP client in android. Any ideas on what
library i can use?. I have tried using Smack API. I installed OpenFire (
xmpp server ) on my computer . Than I can connect OpenFire server
But i cannot login openfire server

       XMPPConnection connection = new XMPPConnection(connConfig);
        try {
            connection.connect();
            Log.i("XMPPClient", "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Connected to " +connection.getHost());
        }
        catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " +
connection.getHost());
            xmppClient.setConnection(null);
        }
        try {
            Log.i("XMPPClient",
"-----------------------------------------------" );
            connection.login("de...@127.0.0.1", "demo1");
            Log.i("XMPPClient", "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Login USER " + connection.getUser());

i got a following errors:
--------------------------------------------------
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Connected to 10.0.2.2
I/XMPPClient(  219): -----------------------------------------------
E/dalvikvm(  219): Could not find class 'java.beans.PropertyDescriptor',
referenced from method
org.jivesoftware.smack.util.PacketParserUtils.parseWithIntrospection
W/dalvikvm(  219): VFY: unable to resolve new-instance 126
(Ljava/beans/PropertyDescriptor;) in
Lorg/jivesoftware/smack/util/PacketParserUtils;
W/dalvikvm(  219): VFY:  rejecting opcode 0x22 at 0x0016
W/dalvikvm(  219): VFY:  rejected
Lorg/jivesoftware/smack/util/PacketParserUtils;.parseWithIntrospection
(Ljava/lang/String;Ljava/lang/Class;Lorg/xmlpull/v1/XmlPullParser;)Ljava/lang/Object;
W/dalvikvm(  219): Verifier rejected class
Lorg/jivesoftware/smack/util/PacketParserUtils;
W/dalvikvm(  219): threadid=19: thread exiting with uncaught exception
(group=0x4001aa28)
E/AndroidRuntime(  219): Uncaught handler: thread Smack Packet Reader (0)
exiting due to uncaught exception
E/AndroidRuntime(  219): java.lang.VerifyError:
org.jivesoftware.smack.util.PacketParserUtils
E/AndroidRuntime(  219):     at
org.jivesoftware.smack.PacketReader.parseIQ(PacketReader.java:576)
E/AndroidRuntime(  219):     at
org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:275)
E/AndroidRuntime(  219):     at
org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
E/AndroidRuntime(  219):     at
org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:76)

----------------------end error----------------------------

Help me please :)
Regards
Tsolmon

-- 

3. how to fix the runtimeexception:android.database.sqlite.SQLiteException: Unable to close due to unfinalised statements

4. Use MediaRecorder for video without preview Surface

5. Android can't read wave files

6. Maintaining Aspect Ratio of Background Image

7. This is Why the Hummingbird Processor in the Samsung Galaxy S is Awesome