Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a ListView from an ArrayList<HashMap<String, String>>

Tags:

java

android

I have an ArrayList<HashMap<Contact, Name>> and I want to populate a ListView with it. Here's my attempt (which is not working)

    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());
    }
}

That last line is throwing an error in eclipse:

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

I've tried everything but I still couldn't make it work, could you help me please?

Thanks in advance.

PS: Full source: https://gist.github.com/4451519

like image 537
kustomrtr Avatar asked Aug 11 '26 01:08

kustomrtr


1 Answers

Just use a 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);

It would be simple (and more logical) if each item of your list contained a similarly formed object, not a different key every time.

I would replace

map.put(key, mount);

by

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

and then the from and to are simply:

String[] from = new String[] { "value" };
int[] to = new int[] { android.R.id.text1 };
like image 185
njzk2 Avatar answered Aug 13 '26 14:08

njzk2