This recipe is taken from the answer to a question on StackOverflow on how to populate a ListView component in Android with data from Kumulos and uses people as an example.

Ingredients

This recipe assumes that you have working knoweldge of the Java programming language and the Google Android SDK.

Recipe

The process to display data in a ListView from any web service is:

  • Call out to the web service and retrieve the list of data
  • Map those objects down to model classes and either hold in memory or persist to disk
  • Create an adapter that is backed by the data on the phone
  • Set the adapter of the list view to be your custom adapter instance

Assuming that you hold the objects in memory, then creating a custom adapter backed by an ArrayList of your model type would be sufficient and is one of the simplest ways to get started. So with Kumulos, you would do something like this:

package com.example.cookbook.simplelistview;        // TODO rename package

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.kumulos.android.jsonclient.Kumulos;
import com.kumulos.android.jsonclient.ResponseHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends ListActivity {

    static class Person {

        public long personID;
        public String firstName;
        public String lastName;

        public static Person createFromGenericMap(Map<String, Object> object) {
            Person p = new Person();

            p.personID = (long) object.get("personID");
            p.firstName = (String) object.get("firstName");
            p.lastName = (String) object.get("lastName");

            return p;
        }

    }

    static class PersonAdapter extends BaseAdapter {

        private List<Person> people;

        public PersonAdapter(List<Person> people) {
            this.people = people;
        }

        @Override
        public int getCount() {
            return people.size();
        }

        @Override
        public Object getItem(int position) {
            return people.get(position);
        }

        @Override
        public long getItemId(int position) {
            Person p = people.get(position);
            return p.personID;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO implement your view
            return null;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Kumulos.initWithAPIKeyAndSecretKey("API_KEY", "SECRET_KEY", this);

        // Call Kumulos
        Map<String,String> params = new HashMap<>();
        Kumulos.call("getPeople", params, new ResponseHandler() {

            // Handle result
            @Override
            public void didCompleteWithResult(Object result) {
                super.didCompleteWithResult(result);

                // Cast generic response down to list of maps
                ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String,Object>>) result;

                // Create a list for the models
                ArrayList<Person> people = new ArrayList<>();

                // Map models from generic objects and add to list
                for (Map<String,Object> personObject : objects) {
                    Person p = Person.createFromGenericMap(personObject);
                    people.add(p);
                }

                // Create adapter with model list
                final PersonAdapter adapter = new PersonAdapter(people);

                // Set adapter on main UI thread
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setListAdapter(adapter);
                    }
                });
            }
        });
    }
}

Limitations

Whilst one of the simplest approaches, it does have some limitations around the handling of orientation changes or navigation away from the activity whilst loading is taking place.

Implementing the view with recycling is also not covered.

Further reading

For more comprehensive list view examples, it might be worth checking out the following resources: