0

我是 android 编程新手,我需要一些帮助。我有以下代码:

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));

我想为列表中的每个条目(城市和国家)显示双行,但我没有运气。使用上面的代码,我只看到国家而不是城市。有什么方法可以将两个适配器都添加到 list_search 以便我可以看到所有数据?

谢谢大家的答案!!

4

2 回答 2

2
  1. 创建一个自定义容器对象,可能是 CountryCityLocation,其中包含几个用于国家和城市名称的字符串。
  2. 创建这些容器的数组并用您的信息填充它。
  3. 创建一个自定义 ArrayAdapter,可能是 CountryCityAdapter,将数组应用到自定义适配器,并将其提供给 getListView.setAdapter。
  4. 覆盖 ArrayAdapter 的 getView 方法以将您的名称字符串应用到自定义列表行视图中的 TextViews。

编辑- 删除了死教程链接

于 2010-10-13T17:33:31.730 回答
1

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.

于 2010-10-13T10:04:44.030 回答