自定义视图布局 - &#34; List <map>无法转换为List <! - ?扩展Map <String,? - >&gt; &#34; </地图>

时间:2014-09-15 19:03:29

标签: java android

我有一个自定义列表视图,允许单击列表中的项目。

运行良好的代码,类似于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&lt;&gt;新规则?


解决方案注意:

回答Aeshang,见下文。请注意,此解决方案也不再需要“(List&gt;)”强制转换,因此该行现在显示为:

SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), 
     myMapList, R.layout.my_list_row, layoutNames, layoutIDs );

眼睛也更容易。

1 个答案:

答案 0 :(得分:2)

你必须改变这一行

List<Map> myMapList = new ArrayList<Map>();

List<Map<String, String>> myMapList = new ArrayList<Map<String, String>>();