Question

I'm new to android programming and I would like some help. I have the following code:

Object[] list_cities = parsedData.getCityname().toArray();
            Object[] list_countries = parsedData.getCountryname().toArray();

            // Display the available locations
            list_search.setAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_2, android.R.id.text1, list_cities));
            list_search.setAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_2, android.R.id.text2, list_countries));

I would like to display double rows for each entry in the list (city and country) but I have no luck. With the above code I only see the countries but not the cities. Is there any way to add both the adapters to the list_search so I can see all the data?

Thank you all for your answers!!

Was it helpful?

Solution

  1. Create a custom container object, maybe CountryCityLocation, with a couple strings for country and city name.
  2. Create an array of these containers and fill it with your information.
  3. Create a custom ArrayAdapter, maybe CountryCityAdapter, apply the array to the custom adapter, and feed it to getListView.setAdapter.
  4. Override the ArrayAdapter's getView method to apply your name strings to TextViews in your custom list row view.

EDIT - removed dead tutorial link

OTHER TIPS

You can bind only one adapter to ListView. If you want to combine adapters you need to implement custom adapter. For example you can inherit SimpleAdapter, provide two simple adapters in constructor and combine data in in getItem method.

This link may help. It shows a listView with multi-lined rows implemented using HashMaps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top