微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 从ArrayList>填充ListView

我有一个ArrayList< HashMap< Contact,Name>>我想用它填充ListView.这是我的尝试(不起作用)

    ArrayList<HashMap<String, String>> lista = new ArrayList<HashMap<String, String>>();

    // Array of strings "titulos"
    String titulos[] = { "Dolar (Transferencia)", "Euro (Transferencia)",
        "Dolar (Efectivo)", "Euro (Efectivo)", "Dolar (cúcuta)",
        "Euro (cucuta)" };

    try {
        JSONObject json = result; // result is a JSONObject and the source is located here: https://dl.dropBox.com/u/8102604/dolar.json
        JSONObject root = json.getJSONObject("root"); 
        JSONArray items = root.getJSONArray("item");
        int j = 0; 

        for (int i = 0; i < items.length(); i++) {
            JSONObject item = items.getJSONObject(i);
            String key = item.getString("key");
            String mount = item.getString("mount");
            if (key.equals("TS") || key.equals("TE") || key.equals("EE")
                    || key.equals("CE") || key.equals("ES")
                    || key.equals("CS")) { // i did this since i only need the items where the key is equal to TS, TE, EE, CE, ES or CS.
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", String.valueOf(i));
                map.put(key, mount);
                lista.add(map);
                System.out.println(titulos[j] + "(" + key + "). BsF = " + mount); // just for debugging purposes
                j++; // add 1 to j if key is equal to TS, TE, EE, CE, ES or CS. In this way i can associate the two arrays (item and titulos)
            }
        }

        ListView lv = (ListView) myMainActivity.findViewById(R.id.listView1); // create a list view 
        lv.setAdapter(new ArrayAdapter<String>(contexto, android.R.layout.simple_list_item_1, lista)); // set adapter to the listview (not working)

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
}

最后一行是在eclipse中抛出一个错误

The constructor ArrayAdapter<String>(Context, int, ArrayList<HashMap<String,String>>) is undefined

我已经尝试了所有的东西,但我仍然无法使它工作,你能帮我吗?

提前致谢.

PS:完整来源:https://gist.github.com/4451519

解决方法:

只需使用SimpleAdapter即可.

String[] from = new String[] { /* all your keys */};
int[] to = new int[] { /* an equal number of android.R.id.text1 */};
listadapter adapter = new SimpleAdapter(contexto, lista, android.R.layout.simple_list_item_1, from, to);

如果列表中的每个项目都包含一个类似的对象,而不是每次都有不同的键,那么这将是简单的(并且更符合逻辑).

我会替换

map.put(key, mount);

通过

map.put("key", key);
map.put("value", mount);

然后来自和来简单地说:

String[] from = new String[] { "value" };
int[] to = new int[] { android.R.id.text1 };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐