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 trialTom Wierman
3,002 PointsArrayAdapter Layout
Hi. I am using Android Studio and the line in the ArrayAdapter declaration:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, mAndroidNames);
gives me an error. Basically it does not recognize "simple_list_item_1". It gives me a list of variables like "abc_...".
I can see the simple_list_item_1.xml in the Android\android-sdk\platforms\android-x\data\res\layout directory.
There must be some sort of import or other statement that tells the project to see this file, but no one is saying so on the various blogs out there.
5 Answers
Joe Brown
21,465 PointsTheres 2 things in your line of code there.. you didnt show what data type your array adapter is, not sure if you have it correct in your actual program. It should be
ArrayAdapter<String> adapter = new ArrayAdapter<String>()
With the simple_list_item_1 problem it should be.. (this, android.R.layout.simple_list_item_1, mAndroidNames)
Notice the android prefix I added there. It is a built in android list provided for you so you have to prefix it with the "android", then it will be recognized
Joe Brown
21,465 PointsOh i see, the forum removes the proper format for the ArrayAdapter data type lol, did it to me so thats what must have happened to you also, never mind. So yea the android prefix will solve your problem there. Good luck
Tom Wierman
3,002 PointsThanks. That seems to be the ticket. Question: Why do we need to specify android.R.layout when the R.menu works?
Joe Brown
21,465 PointsI guess cause R.menu is provided for us by default right from the start. All the other things available to us by the android platform we have to go fetch them so to speak, so it requires the android prefix. Like in the xml there is built in things provided by android you can use but you have to specify with "@android:" to be able to access them
Tom Wierman
3,002 PointsThanks