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

ListView

Hi I i can construct a basic ListView but im struggling to have my items clicked on to show another another activity.

Eg: I have a list of countries and a list on main spoken languages, the countries are in my list and the when a user clicks on any country a new activity will appear with a textview of what main language is spoken in that country.

how do you arrange elements of an array to show another element of another array?

3 Answers

Roland Kalmogo
Roland Kalmogo
25,657 Points

You're welcome! In the case of vegetables, there two choices: first, you can create a class vegetables like this:

public class Vegetable{ String description; String[] vitamins/// if you want your vitamins to contain attributes in addition you can create a class for them }

then in you list view, you'll use an array of Vegetables as content. It's the same process as in your last content.

second, you can use a map to map each vegetable to a list of vitamins. Map myData=new HashMap<String//for your vegetables, List<String>//for the list of vitamins for each vegetable>(); All the next is like what we spoke about.

Happy coding!

Roland Kalmogo
Roland Kalmogo
25,657 Points

Hi,

I think that a good way to deal with your problem is to use a country class where you can find the main spoken language.

But if you still want to keep your two arrays, you can write a method which can bring you the spoken language from a given country.

public String findTheMainLanguageOf(String countryName){ // I guess there are alot of if here :) otherwise, you find a way to map country to language// } To easily achieve you job, you can use a hashmap to map the country to their main language.

Map myData=new HashMap<String, String>(); /// for each country, add the spoken language myData.put("myFirstCountryName","correspondingSpokenLanguage");

In you array adapter (I guess you use a basic array adapter for you list view activity), you just have to fill the list view with of keys of you map(which represent the list of your contries).

In you onClick method for each view in the list, you retrieve the corresponding language in the map from the countryName.

@OnClick public void startSpokenLanguageActivity(All the parameters) { Intent intent = new Intent(this, SpokenLanguageActivity.class); intent.putExtra(COUNTRY_SPOKEN_LANGUAGE, myData.get(country)); startActivity(intent); }

Hence, in you spoken language activity, you can retrieve your spoken language and disply it(from the intent).

I hope it helped ;)

thank you for your answer, what if there is a list of vegetables and when you click on a it you get content on what vitamins that vegetable contains.