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 Build a Blog Reader Android App Adapting Data for Display in a List Filling Our String Array and Creating the Adapter

Joel Brewster
Joel Brewster
19,064 Points

Please give me some tips to progress from "Filling Our String Array and Creating the Adapter" 2 of 3.

I know something is wrong with the ArrayAdapter and the generic <>. I'm not sure how to fix it. Any tips? Please explain if you've got time. Many thanks in advance.

[EDIT]: Also, how do I format the code on the forums?! <code> [code] ```?

JSONArray jsonVideos = jsonData.getJSONArray("videos");
String[] titles = new String[jsonVideos.length()];

for(int i = 0; i < jsonVideos.length(); i++) {
    JSONObject videos = jsonVideos.getJSONObject(i);
    String title = videos.getString("title");  
    titles[i] = title;
}

Adapter
ArrayAdapter<titles> adapter = new ArrayAdapter<titles> (this, android.R.layout.simple_list_item_1, jsonVideos) ;
setListAdapter(adapter);


//After your for loop, declare an ArrayAdapter variable named 'adapter' and initialize it with its constructor. 
//ArrayAdapter adapter = new ArrayAdapter

//Remember that ArrayAdapter is a generic, meaning that the type of the items in the array needs to be specified in angle brackets.
//ArrayAdapter adapter <?> = new ArrayAdapter <?>
//? Is String in the videos but that doesn't work here and I don't understand why.

//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.
//(this, android.R.layout.simple_list_item_1, jsonVideos) ; setListAdapter(adapter);

6 Answers

Aaron Arkie
Aaron Arkie
5,345 Points

Hello you are so close!!! pretty exciting, Okay first off you were right on ArrayAdapter<String>! this is correct because the ArrayAdapter will hold an Array of Strings. However, i believe where you went wrong is that you used jsonVideos as the third parameter for the ArrayAdapter. jsonVideos i believe is an object used to gain access to the strings in the String[] array. SO, let me give you a hint! Since the third parameter is looking for a literal string Array, you must use that variable which holds strings. so swap out jsonVideos in the ArrayAdapter parameter, then put in the String Array(hint: String [] --> ?) dont include String [] just the variable. Also take out setListAdapter(adapter); as it is not needed yet i believe.

I will format your code so you can see where you went wrong(: just fill in the blanks!

JSONArray jsonVideos = jsonData.getJSONArray("videos");
String[] titles = new String[jsonVideos.length()];

for(int i = 0; i < jsonVideos.length(); i++) {
    JSONObject videos = jsonVideos.getJSONObject(i);
    String title = videos.getString("title");  
    titles[i] = title;
}

Adapter
ArrayAdapter<?> adapter = new ArrayAdapter<?> (this, android.R.layout.simple_list_item_1, ?) ;
setListAdapter(adapter); //i believe this is not needed yet

I hope this helps goodluck!

[EDIT]: Also, how do I format the code on the forums?! [code] ```?

I have no idea. I'm just copying and pasting them.

Are you sure you are gonna set "jsonVideos" to adapter?

code here

type three backticks ``` and then the language specifier "android" or "objective-c" or "java" etc...

Marc-Oliver Gern
Marc-Oliver Gern
8,747 Points

I don't get it either. Why can't I add <String>?

Got it now: ... ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, titles); // "titles" references to the string array. "<String> is the data type the adapter expects

And you can set the name of variable to whatever you want, but I think "videos" may cause some confusion. Since what "jsonVideos.getJSONObject(i)" gets you is one single JSONObject which is a collection of information such as, for example, title, length, or uploader: i'm not sure without looking at the jsonData, of "one" video. Therefore the name should be "video" instead of "videos".

Joel Brewster
Joel Brewster
19,064 Points

Thank you so much for the responses!

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

Did the trick.