Precious Roy
Precious Roy

Reputation: 1086

Android: Multiple-Column ArrayList to Listview

I am trying to figure out the best method of displaying a json result in a listview. I have the following json response coming from a WCF RESTful service:

[
    {
        "Class": "Lorem",
        "Company": "Ipsum",
        "Id": "XXXX",
        "Name": "Avent"
    },
    {
        "Class": "Consectetur",
        "Company": "Adipiscing",
        "Id": "YYYYY",
        "Name": "Nulla"
    }
]

I've read several examples of arraylist to listview, but none with a multiple column issue I'm trying to get around. I want the column to display the "Name" field large, and the "Company" name beneath it in smaller text. I'd like to store the Id as some sort of key for when the user clicks on the item, to call it in another function, but I'm not sure how best to do that.

Group Class:

public class Group {

    private String Id;
    private String Name;
    private String Class;
    private String Company;   
}

gson parsing statement:

ArrayList<Group> groups = gson.fromJson(jsonString, new TypeToken<ArrayList<Group>>() {}.getType())

I've been using an ArrayAdapter, but I get the feeling I may need to create a custom Adapter for this. Do I have to use a HashMap to make this work? Do I need to transform my arraylist somehow?

Upvotes: 1

Views: 3477

Answers (1)

Dan S
Dan S

Reputation: 9189

ArrayList is the first step in the right direction, next use a HashMap per row, and finally add that to your SimpleAdapter.

More Info

Each row is made of several views which correspond to several data points. To keep all this data points organized we place them into a HashMap, 1 hash map per row. To present several rows of data we have an ArrayList to hold each row of data, 1 element (of HashMap) per row. There are two steps to put it all together. First passing the ArrayList and Data to View mapping in the constructor of the SimpleAdapter. Second customize the SimpleAdapter.ViewBinder as needed. The binder can automatically handle text for TextViews and drawable resource id's for ImageViews.

A code example here is a bit too large but check it out here.

Upvotes: 2

Related Questions