Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.