A JBoss Project
Red Hat

Aerogear Guides Tutorials to help get you off and running.

AeroGear Android Pagination

The Android AeroGear libraries implement paging as defined in the pagination spec. We also have common use cases for the basic tasks in the Android sections of the paging usage document.

These two resources should be enough to get you up and running with Aerogear controller based services. However, if you have special requirements or legacy services AeroGear's pagination framework can also be extended to support some pretty gnarly use cases.

AeroGear ships with support for selecting data from the server response and using that data as a relative URI for locations of the "next" and "previous" pages of data. You can enable paging by configuring a PageConfig object and setting it on the Pipe using PipeConfiguration.pageConfig. When this option is enabled, Pipe.read and Pipe.readWithFilter will pass PagedList instances to Callback.onSuccess instead of plain Lists. You can call PagedList.next and PagedList.previous for load the next and previous data sets respectively.

Let's say we have a service which returns a page of data in the following format:

Response
{
    "data": {
        "after": "?after=t3",
        "before": ?before=t1,
        "children": [
                {"data":"data1"},
                {"data":"data2"},
                {"data":"data3"},
                {"data":"data4"}
        ]
    }
}

The 'after' parameter defines the page of data which comes after the current page. The 'before' parameter likewise defines the page which comes before the page of data. If these fields are relative URIs, AeroGear can use them for paging data.

The PageConfig object has parameters for setting WHERE paging location data is (pageConfig.setMetadataLocation), and which fields correspond to which data pageConfig.setNextIdentifier, pageConfig.setPreviousIdentifier, and several other parameters specified in the JavaDoc.

ExampleApplication.java
void onCreate() {
     pipeline = new Pipeline(BASE_URL);

     PageConfig pageConfig = new PageConfig();
     pageConfig.setLimitValue(25);
     pageConfig.setMetadataLocation(PageConfig.MetadataLocations.BODY);
     pageConfig.setNextIdentifier("data.after");
     pageConfig.setPreviousIdentifier("data.before");

     MarshallingConfig marshallingConfig = new MarshallingConfig();
     marshallingConfig.setDataRoot("data.children")

     ResponseParser responseParser = new GsonResponseParser();
     responseParser.setMarshallingConfig(marshallingConfig);

     RestfulPipeConfiguration config = PipeManager.config("listing", RestfulPipeConfiguration.class)
                .withUrl(BASE_URL)
                .responseParser(responseParser))
                .pageConfig(pageConfig);

     Pipe<Data> pipe = config.forClass(Data.class);

}

And now when we call read or readWithFilter our CallBack's onSuccess method will be passed a PagedList instance.

ReadCallback.java
public class ReadCallback extends AbstractActivityCallback<List<Data>> {
    public void onSuccess(List<Data> data) {
        ExampleActivity activity = getActivity();
        activity.setData((PagedList<Data>) data);
        activity.setListAdapter(new ArrayAdapter<Data>(activity,
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                data));
    }

    public void onFailure(Exception e) {
	//Handle error
    }
}

PagedList has next and previous methods. Both of these methods consume a Callback and call Pipe.readWithFilter with a ReadFilter object designed to reference the particular page requested and the callback provided to the method on PagedList

ExampleActivity
    public void nextPressed(View button) {
        data.next(readCallback);
    }

    public void previousPressed(View button) {
        data.previous(readCallback);
    }

redhatlogo-wite