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 trialAdlight Sibanda
5,701 Pointsandroid 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).
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);
}
}
2 Answers
Nicolas Hampton
44,638 PointsSet the name of the ArrayAdapter to 'adapter'. Adapters are just like any other object, each instance needs a name of it's own.
Nicolas Hampton
44,638 Pointsoh, I see. You did that, but there's no space in between.
Nicolas Hampton
44,638 Pointsalso, that all goes in the onCreate method.
Nicolas Hampton
44,638 PointsLeave the array declaration where it's at, but set it to private as well.
Adlight Sibanda
5,701 Pointsit worked, i appreceate ;-)
Nicolas Hampton
44,638 PointsI do what I can. Take care!
MUZ140663 Question Ngwarati
11,782 Pointsimport 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); } }
MUZ140663 Question Ngwarati
11,782 Points<String> between ArrayAdapter<String> adapter
Adlight Sibanda
5,701 PointsAdlight Sibanda
5,701 PointsChallenge Task 2 of 2
Set adapter as the default adapter for AppListActivity using the setListAdapter() method, which takes an adapter as a parameter.