自定义视图布局 - &QUOT;清单&LT;地图&GT;不能被转换为List&LT ;?扩展地图&LT;字符串,&GT;&GT; &QUOT; [英] Custom View Layout - " List<Map> cannot be converted to List<? extends Map<String,?>> "
问题描述
我有允许对列表中的项目单击自定义列表视图。
I have a custom list view which allows clicking on items in the list.
在code的效果很好,类似于谷歌的自己的网站的例子很多,并成功编译,直到最近的Java,是这样的:
The code that worked well, similar to many examples on google's own site, and compiled successfully until more recent java, is this:
List<Map> myMapList = new ArrayList<Map>();
Map<String, String> myMap = new HashMap<String, String>();
private ListView myListView;
for (int i = 0; i < mydata.length(); i++) {
JSONObject t = mydata.getJSONObject(i);
myMap = new HashMap<String, String>();
myMap.put("field_1", t.getString("field_1"));
.... (more of these)
myMapList.add(myMap);
}
String[] layoutNames = new String[] { "field_1", "field_2", "field_3",
"field_4", "field_5" };
int[] layoutIDs = new int[] { R.id.field_1, R.id.field_2, R.id.field_3,
R.id.field_4, R.id.field_5};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
(List<? extends Map<String, ?>>) myMapList, R.layout.my_list_row,
layoutNames, layoutIDs );
myListView.setAdapter(adapter);
起初,警告被赋予,但现在它是一个编译时错误。
At first, warnings were given, but now it is a compile-time error.
error: incompatible types: List<Map> cannot be converted to List<? extends Map<String,?>>
因此,如何我养活我的字符串/整数组合成列表与LT;>新规则
So how to I feed my String / Integer combo into List<> with the new rules?
解决方法注意:
答转到Aeshang,见下文。请注意,此解决方案还摆脱的需要,为(名单>)剧组,让行现在如下所示:
Answer Goes to Aeshang, see below. Note that this solution also gets rid of the need for the "(List>)" cast, so that line now reads:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
myMapList, R.layout.my_list_row, layoutNames, layoutIDs );
对眼睛更容易了。
Easier on the eyes, too.
推荐答案
您需要更改此行
List<Map> myMapList = new ArrayList<Map>();
到
List<Map<String, String>> myMapList = new ArrayList<Map<String, String>>();
这篇关于自定义视图布局 - &QUOT;清单&LT;地图&GT;不能被转换为List&LT ;?扩展地图&LT;字符串,&GT;&GT; &QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!