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

Android Android Lists and Adapters (2015) Standard ListViews Using an ArrayAdapter

android lists and adapters

Inside the onCreate() method, declare an ArrayAdapter named adapter and initialize it with its constructor. Remember that ArrayAdapter is a generic, meaning that the type of the items in the array needs to be specified in angle brackets. The constructor takes three parameters: 1) the context (use this), 2) the layout (use android.R.layout.simple_list_item_1) and 3) the array to adapt (use mApps).

AppListActivity.java
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class AppListActivity extends ListActivity {

    String[] mApps = {"Instagram","Pinterest","Pocket","Twitter" }; 
ArrayAdapteradapter=new ArrayAdapter(this android.R.layout.simple_list_item_1, mApps); 
setListAdapter(adapter);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_list);
    }
}

Challenge Task 2 of 2

Set adapter as the default adapter for AppListActivity using the setListAdapter() method, which takes an adapter as a parameter.

2 Answers

Set the name of the ArrayAdapter to 'adapter'. Adapters are just like any other object, each instance needs a name of it's own.

oh, I see. You did that, but there's no space in between.

also, that all goes in the onCreate method.

Leave the array declaration where it's at, but set it to private as well.

it worked, i appreceate ;-)

I do what I can. Take care!

import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter;

public class AppListActivity extends ListActivity {

String[] mApps = {"Instagram","Pinterest","Pocket","Twitter" }; 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this android.R.layout.simple_list_item_1, mApps); setListAdapter(adapter); } }

<String> between ArrayAdapter<String> adapter