How does ListActivity create multiple list items

by Mark Murphy » Wed, 02 Jul 2008 01:30:59 GMT


Sponsored Links
 > My understanding of the documentation is that to give a custom look to

Close. There are actually two layouts you provide: one for the activity,
and one for the rows.

The one for the activity needs to have a ListView with the android:id of
"list". You connect this layout to the activity via setContentView(), like
you would for any other activity.

The constructor of the Adapter you use with your ListView will take the
R.layout name of a second layout, this one for the rows.

You are also welcome to override getView() on the Adapter, which will
ignore the second layout and allow you to construct the rows by whatever
means you see fit (manual constructors, ViewInflate, etc.).

Later this month, I'll be writing up a tutorial on AndroidGuys about all
this.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ -- Available Now!


--~--~---------~--~----~------------~-------~--~----~



How does ListActivity create multiple list items

by Disco Stu 010 » Wed, 02 Jul 2008 02:57:44 GMT


 Hey Mark,

Thanks for the explanation. Most helpful. I now understand that one  
can have any view to render a row by overriding the getView() method.

My original question though still stays. If given a "resource id" for  
a view, how can one create multiple instances of that view? Can I  
simply call findViewById() multiple times to get different instances  
or do they return the same copy of the View object?

Thanks,
DS.

PS: Looking forward to your tutorial - DS






--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How does ListActivity create multiple list items

by Mark Murphy » Wed, 02 Jul 2008 03:20:12 GMT


 > Thanks for the explanation. Most helpful. I now understand that one

Or by supplying your own layout XML identifier
(R.layout.somethingoranother), if the rows are reasonably simple. I'm a
bit of a control freak, so I prefer overriding getView(), but that's just
me.


 http://code.google.com/android/reference/android/view/ViewInflate.html 

ViewInflate#inflate() will create a new View based on a supplied resource ID.

For example:

ViewInflate inflater=getViewInflate(); // assumes you're in an Activity
View view=inflater.inflate(R.layout.tourview_std, null, null);
TextView distance=(TextView)view.findViewById(R.id.distance);
ImageView turn=(ImageView)view.findViewById(R.id.turn);
ImageView marker=(ImageView)view.findViewById(R.id.marker);
TextView waypoint=(TextView)view.findViewById(R.id.waypoint);

Here, I inflate R.layout.tourview_std, then use findViewById() to get at
the innards. I can then set the text and images as needed.

For better performance, examine the View convertView parameter passed into
your getView() callback. If it's the right View to use (e.g., you're only
inflating one type of row), then just reuse that instance, since it's one
you inflated earlier. If it's null or the wrong view (e.g., you've got
several types of rows, each with different layouts), inflate yourself a
new one. The inflation process isn't the speediest, so the fewer inflates
you can do, the better.

Hope this helps!

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ -- Available Now!


--~--~---------~--~----~------------~-------~--~----~



How does ListActivity create multiple list items

by Disco Stu 010 » Wed, 02 Jul 2008 04:03:17 GMT


 Hi Mark,

Thanks for the detailed explanation. This helps tremendously.

Can you please also clarify what do you mean by "use the same instance  
as the convertView" parameter? Dont I need a new View instance for  
every cell being displayed? i.e., if displaying 5 TextView rows at the  
same time, then do we need need 5 different instances of TextView? OR  
is it the case that the getView() method is called only when the View  
is about to be drawn and therefore a single instance will suffice for  
displaying the whole list ?

Thanks,
Nitin.







--~--~---------~--~----~------------~-------~--~----~



How does ListActivity create multiple list items

by Mark Murphy » Wed, 02 Jul 2008 04:19:37 GMT


 > Can you please also clarify what do you mean by "use the same instance

Ideally, Android and your activity will only create as many row Views as
can be physically displayed by the ListView.

Suppose you have an Adapter that wraps a set of 50 objects, but the
ListView only has room to show 7 of those at a time. When your activity
launches, I believe your Adapter will have getView() called 7 times with a
null convertView, which you will wind up populating with the first 7
objects' data out of the 50 available objects. Then, if the user scrolls
the ListView, your getView() will start being called with convertView
sometimes being non-null, meaning the ListView wants to fill in an
existing row's View with new data.

I imagine the precise details on when getView() will be called with a
possible-to-recycle convertView will vary as Android evolves. However, if
you get a non-null convertView, Android is asking you to recycle that View
if possible. Either return convertView (after modifying the contents to
reflect the data identified by the supplied position), or return a
brand-new View, as your activity sees fit.

At the outset, it may be simplest to ignore convertView and just inflate
new Views on each getView() call. If performance suffers, you can always
try reusing convertView.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ -- Available Now!


--~--~---------~--~----~------------~-------~--~----~



How does ListActivity create multiple list items

by Disco Stu 010 » Wed, 02 Jul 2008 04:49:00 GMT


 Hi Mark,

I think I got it now :)

Thanks a bunch for all your help.

Regards.






--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Use your Machine as ADK with libusb

Hi everyone,

I am really e{*filter*}d about the ADK but do not yet have a USB-Host
Board. If you are like me and want to start testing and programming
over the weekend then you can do this using your computer as
accessory :)

If you are running Linux then you can just use my little piece of C
code to communicate with your Android phone over USB:
 http://www.***.com/ 

I believe it is a handy way to play around with the new libraries and
lets you quickly adapt your source code. I used the C code from the
official Arduino example as a rough guideline for the initial device
setup. You might also use the asynchronous libusb API functions to
enhance the protocol - for the sake of simplicity I only used the
synchronous ones :)

Cheers
Manuel

-- 

2. AVD Emulator Opens Wrong Dpi

Hello,
I have created an AVD virtual device representing the Samsung Vibrant.
I choose all the default settings: Target Android-2.1-update 1 level
7, 16 MB SD, WXVGA, and I accept 240 density and 24 MB heap.

About half the time when I start the virtual device via AVD it comes
up in the wrong density. The icons are too small, etc. I close the
emulator and restart via AVD. I may have to do this two or three times
before it comes up with the right density.

I have deleted and re-created the virtual device in AVD several times.
Each time it works once or twice, then reverts to this mode of coming
up with the wrong density.

What am I doing wrong?

Thanks,
Barry.

-- 

3. Gallery image should navigate with textview

4. Replay monkey execution

5. How to convert dimensions in to dp?

6. [WTI] Gtab upgrade 2.3.3

7. help with new class declaration