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. Finding the name of installed applications

Given the following code, I know how to get a list of ResolveInfo that
describes all of the installed applications, but I am not sure how to
get the names of the applications as seen in the home menu.

PackageManager pm = this.getPackageManager();

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

Currently I can get the packageName and className of a given
application as such:

ResolveInfo re = list.get(position);
String packageName = re.activityInfo.packageName;
String className = re.activityInfo.name;

But as far as getting the same name that you would find if you were to
do

Context ctx = this;
Resources appR = ctx.getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name",
"string", ctx.getPackageName()));

I cannot seem to figure out how to just get the name.  Obviously this
is possible because there are applications that replace your home
screen and so forth, but I have been searching for hours to no avail.

Thanks!

-Sam


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

2. add my Launcher.apk into emulator

hi

i try to modify the Launcher in mydroid/package/apps/Launcher
and i try to use it as an APK so i dont have to mess up the original
system image

so i copy it to another project , say mydroid/package/apps/MyLauncher

i have changed the package name in case it will conflict with the
original Launcher

and because some files use android.internal.R
using 'android' to install/update is not feasible
hence i build my MyLauncher inside mydroid , and i modify the
LOCAL_PACKAGE_NAME in Android.mk

when i build out the MyLauncher.apk

i try to install it with adb install
but it complains

Failure [INSTALL_FAILED_ALREADY_EXISTS]

but i am sure i havent installed this APK before

and if i use $>adb install -r , it will complain

Failure [INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES]

so i would like to ask what's the correct way to install my
MyLauncher.apk into emulator

thanks
--~--~---------~--~----~------------~-------~--~----~

3. Booting from SD card

4. ActivityGroup + embedded Activity search window

5. Android Market on web vs G1

6. Android Runtime Errors!!! NEED HELP!!!

7. android service utilizing custom usb device