Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kunal Patel
1,448 PointsAndroid: The ListView and simple_list_item_2
In my layout file I have: <pre><code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#e1e1e1" android:baselineAligned="true">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|center_vertical"
android:divider="#000000" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textColor="#000000"/>
</LinearLayout>
</code></pre>
How do I know to use this as the id for ListView ' android:id="@android:id/list"'?
And how does this:
SimpleAdapter simpleAdapter = new SimpleAdapter(this, blogPosts,
android.R.layout.simple_list_item_2,keys,ids);
Actually know to update the @android:id/list component?
Thank you!
1 Answer
Ben Jakuben
Treehouse TeacherRegarding the ID, go back to this video (around the 5:45 mark) for an explanation of where that comes from and how to use it.
For your 2nd question about how does the adapter know to update the list, that link actually occurs in the setListAdapter(simpleAdapter) method that comes after the declaration you pasted. The details of how the list and adapter work together are all done behind the scenes when we use a ListActivity like this. We need to use that specific ID to allow it to work like this.
Kunal Patel
1,448 PointsKunal Patel
1,448 PointsAh this clarifies quite a bit, for part 1:
Its the fact that the Activity extends ListActivity which is how Android knows to wire this view to the activity?
Does this mean that the methods overridden for ListActivity are used to populate the view in run time?
As for the second part, more or less as I build my own apps, how do I look for things like this?
I found the xml documentation on simple_list_item_2 which you helped me find. I am just not entirely sure who one knows @android:id/list and simple_list_item_2 go together.
If you started a second Activity via an Intent which extended ListActivity, and for some bad programming convention used this layout, it would populate that UI component with its list as well?
Kunal Patel
1,448 PointsKunal Patel
1,448 PointsFound an answer to part 2.
https://developer.android.com/reference/android/app/ListActivity.html
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherHooray! Hopefully I can help with your other questions, too.
Correct. ListActivity contains code that looks for a ListView with that specific ID and then hooks everything up for us.
Sort of (if I understand your questions). The adapter populates the ListView, using the layout we provide. If we want to customize the layout then we need to provide our own layout XML for each item in the list as well as override some methods in a custom adapter. We'll do this in an upcoming Android project.
You will want to Google and search the docs. Learning how to search them is somewhat of an art. :) You just have to get somewhat familiar with how they're organized and you can start to learn where to look or how to search for things by key words.
Try to get familiar with the topics here in the API Guides. For ListViews, specifically you want to look at Layouts > ListView. This link is somewhere in my Teacher Notes, too. :)
I'm not sure I understand this last question - can you clarify? Or maybe the other information above will help answer it.