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


Michael Varner
Michael Varner
2,187 Points

Having trouble understanding how ArrayAdapter identifies the specific ListView in activity_main_list

In the lesson 'Adding Data for the list", an ArrayAdapter is used to take in an array and adapt it for a list:

'''java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mAndroidNames); setListAdapter(adapter); } '''

I understand how '''java setContentView(R.layout.activity_main_list); ''' Identifies which content layout to use.

What I don't understand is how in the onCreate method, it knows to use a particular ListView in activity_main_list.xml:

'''xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.teamtreehouse.blogreader.MainListActivity$PlaceholderFragment" >

    android:layout_alignParentTop="true" >

<TextView android:id="@android:id/empty"
    android:text="No items to display!" />


I can assume that it simply finds the solitary ListView that's in there currently, but what if there is more than one ListView within the relative layout?

What in the onCreate method is identifying that exact ListView?

1 Answer

Theodore Kruczek
Theodore Kruczek
8,800 Points

It is actually before onCreate. It is the part where you extended as ListActivity. This will look for the list view with @android:id/list as its id.

If you wanted to have more than one ListView in a single activity, you would not extend as ListActivity, but instead extend it as a normal activity. From what I have seen on StackOverflow, it isn't terribly difficult to use multiple ListViews, but I don't want to give you a specific answer and mislead you if I am wrong.