[英]how to set an ArrayList<HashMap<String,String>> in an ArrayAdapter
Can we set an ArrayList
containing HashMap
into an ArrayAdapter
? 我们可以将包含
HashMap
的ArrayList
设置为ArrayAdapter
吗?
I'm using: 我正在使用:
ArrayList<HashMap<String,String>> items = new ArrayList<HashMap<String,String>>();
ArrayAdapter<ArrayList<HashMap<String, String>>> ad = new ArrayAdapter<ArrayList<HashMap<String,String>>>(this, android.R.layout.simple_list_item_1,items);
but this gives me an error saying: 但这给了我一个错误说:
The constructor ArrayAdapter>>(searchname, int, ArrayList>) is undefined
构造函数ArrayAdapter >>(searchname,int,ArrayList>)未定义
ArrayAdapter
wants to know the type of the items inside the list, not the type of the list itself. ArrayAdapter
想知道列表,该列表本身不是那种里面的物品的类型。 Remove the enclosing ArrayList<>
and it should work. 删除封闭的
ArrayList<>
,它应该工作。
ArrayList<HashMap<String,String>> items =
new ArrayList<HashMap<String,String>>();
ArrayAdapter<HashMap<String, String>> ad =
new ArrayAdapter<HashMap<String,String>>(this,
android.R.layout.simple_list_item_1,items);
have a look at this link Android Array Adapter with ArrayList and ListView not updating when the arraylist is changed . 看看这个链接Android Array Adapter与ArrayList和ListView在更改arraylist时不更新 。
basically you need to add your ArrayList into the ArrayAdapter through the add method. 基本上你需要通过add方法将ArrayList添加到ArrayAdapter中。
You can convert the type HashMap<String,String>>
to a class. 您可以将类型
HashMap<String,String>>
转换为类。 For example, if the keys of the hashmap are countries and the values are lenguages, you can make a class called "Country" with this two variables (country,language). 例如,如果hashmap的键是国家,而值是lenguages,则可以使用这两个变量(国家/地区,语言)创建一个名为“Country”的类。 Then the AdapterClass have to extend the
ArrayAdapter<Country>
, in the getView method you can access both variables of each Country. 然后AdapterClass必须扩展
ArrayAdapter<Country>
,在getView方法中,您可以访问每个Country的两个变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.