我有一个自定义列表视图,该视图允许单击列表中的项目。运作良好的代码类似于Google自己网站上的许多示例,并且可以成功编译直到使用最新的Java,如下所示: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);最初,给出了警告,但现在是编译时错误。error: incompatible types: List<Map> cannot be converted to List<? extends Map<String,?>>那么,如何使用新规则将String / Integer组合提供给List ?解决方案说明:回答前往Aeshang,请参阅下文。请注意,此解决方案还摆脱了对“(List>)”强制转换的需要,因此该行现在显示为:SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), myMapList, R.layout.my_list_row, layoutNames, layoutIDs );眼睛也更容易。 最佳答案 您必须更改此行List<Map> myMapList = new ArrayList<Map>();至List<Map<String, String>> myMapList = new ArrayList<Map<String, String>>();关于java - 自定义 View 布局-“List <Map>无法转换为List <?扩展Map <String,?>>”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25855056/
10-10 19:57
查看更多