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
ms2030
2,973 Pointscode challenge problems: ArrayList<HashMap<String, String>>
I'm not sure what's wrong but I know that I'm clueless about the code that I'm typing in here. This lesson went over my head.
I think this is a case where I need a comment for each line as I don't find the code self-explanatory.
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class WebsiteListActivity extends ListActivity {
public static final String KEY_NAME = "name";
public static final String KEY_URL = "url";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_website_list);
String[] keys = { KEY_NAME, KEY_URL };
int[] ids = { android.R.id.text1, android.R.id.text2 };
// Add code here!
HashMap<String, String> hash1 = new HashMap<String, String>();
hash1.put(KEY_NAME, "one");
hash1.put(KEY_URL, "two");
HashMap<String, String> hash2 = new HashMap<String, String>();
hash2.put(KEY_NAME, "three");
hash2.put(KEY_URL, "four");
ArrayList<HashMap<String, String>> websites = new ArrayList<HashMap<String, String>> ();
String[] keys = { KEY_NAME, KEY_URL };
SimpleAdapter adapter =
new SimpleAdapter(this,websites,android.R.layout.simple_list_item_2,keys,ids);
websites.add(hash1);
websites.add(hash2);
}
}
2 Answers
Hasan Tugkan Kibar
2,230 PointsLet me give it a try. In your code you create an array of Strings called keys and array of ints called ids. Your keys string hold the keys(duh.) and your ids string holds the ids for your textView's which you will put the author and title data to display. After that you create 2 HashMaps called hash1 and hash2. These HashMap's connect a String to a String like a dictionary. If I were to say KEY_NAME, "one" for instance it would be something like "name" : "one". And the reason you use KEY_NAME and KEY_URL is to tag these data to be able to access it later(You'll understand in a second) After this you create an array of hashmaps (arraylist to be certain but that just means that our array is dynamic so they are not of a fixed length) and call it websites. You also re-state your keys array(not needed as much as I know). After this you create a simple adapter but before that let's look into our array of hashmaps. When you use websites.add(hash1/2) what you do is adding hashmaps to your array of hashmaps. Fair enough and it becomes something like this;
"name" : "one"
"url" : "two"
"name" : "three"
"url" : "four"
See how it looks? Well you are using this with your adapter to put the data on screen. In your adapter you state five things. Context, data, resource, from and to. Context is where the View thing associated with this adapter is running. In our case it's the same class so we say this. Data is the data to display(duh.). Which is the array of hashmaps named websites. Resource is the how the data should be displayed. You specify the layout and that layout MUST BE defined in 'to'. From is our tags to add the data on the display(More to this in a sec.) To is the displays that are going to display out data.
In this case you use keys[] as your from and ids[] as your to. What you want to do is to display names as the first text and url as the second one right? Well that's why you used hashmaps, that's why you literally TAGGED your data. Remember your keys array and ids array? They are like this:
KEYS = KEY_NAME , KEY_URL
IDS = android.R.id.text1, android.R.id.text2
You see what your adapter does is that it associates every key[i] to ids[i]. (From and To). So your KEY_NAME and text1, KEY_URL and text2 is being associated. But that's not all of it! Your from was tags remember? Well Android takes your TAGS and goes to your data part(websites array). Takes the one with the KEY_NAME tag and sends it to text1. Since you are using a 2 item list layout it also looks for key[1] and ids[1] which means it also looks for the KEY_URL tagged item and sends it to ids[1] which is our beloved text2. And goes on and on until your array is finished. (Remember, you are in an array of hashmaps so that was just the first item in the array. The same procedure is repeated 'till the end of the array.)
And that's about it really. Don't hesitate to ask if you have any questions! ^^
Grinnell Appdev
23,708 PointsA couple of things:
1) You created a duplicate String Array variable ("keys") just below where you declare your ArrayList variable.
2) You're adding your hashMaps to 'websites' after you adapt it - this means that your adapter is simply adapting a null ArrayList.