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 Adding Secondary Data with a SimpleAdapter

Noah Schill
Noah Schill
10,020 Points

Define Hashmap?

It is telling me to define these HashMaps using KEY_NAME as the key and to use KEY_URL as the value. It is saying I did it wrong. Did I?

WebsiteListActivity.java
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class WebsiteListActivity extends ListActivity {

    public static final String KEY_NAME = "myName";
    public static final String KEY_URL = "myUrl";

    @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> site1 = new HashMap<String, String>();
      HashMap<String, String> site2 = new HashMap<String, String>();
      site1.put(KEY_NAME, KEY_URL);
      site2.put(KEY_NAME, KEY_URL);
    }
}

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

It's expecting both keys to be paired with values that you provide for both HashMaps. Since this is to represent a website, it'll look something like this:

site1.put(KEY_NAME, "example");
site1.put(KEY_URL, "example.com");
Noah Schill
Noah Schill
10,020 Points

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> site1 = new HashMap<String, String>();
  HashMap<String, String> site2 = new HashMap<String, String>();
  site1.put(KEY_NAME, "example");
  site1.put(KEY_URL, "example.com");
}

}

I tried that but I get an error saying: Make sure you add the name of the website using the KEY_NAME variable for both HashMap variables.
;/

Dan Johnson
Dan Johnson
40,533 Points

You need to define the key value pairs for site2 as well.

Noah Schill
Noah Schill
10,020 Points

Oh! I understand now. Thank you!