Splicer Blog

Data Model Automation

Android MVC Design

Android MVC Design
I found a good reference for Android MVC design (“Programming Android”) which states that the Adapters loosely serve as the Controller. For the Model layer, we propose that the data model should be flexible and ideally controlled from a single file. Hence we generate the pojos for the smartphones from the model descriptor in such a way that the model code is independent, and does not overlap with manually-written code; our model is “orthogonal” to controller code such that our code generators do not over-write any programmer-written code.

Anrdoid Adapters
Once the project is setup correctly, your Adapters can instantiate the generic JsonFetcher to fetch data from the network:

private void loadData(){ 
    JsonFetcher<User[]> asyncTask = new JsonFetcher<User[]>(this, User[].class, "/users");
    asyncTask.execute(new Object[0]);//pass no arguments
}

You then implement the SoapDataRequestor interface…

public class MyActivity extends Activity implements WebserviceRequestor<User[]> {
    ...
}

and walah: the service returns an array of your Users via the onNetworkSuccess callback. Typically, you would then assign that array to a class member variable and then invalidate the view.

public void onNetworkSuccess(User[] retval) {
}

How it works
The sample Android project structure is very clean: the classes in the models.ddm package are generated. So try generating your own classes and then instantiate JsonFetcher with your new type! It also allows us to write generic Adapters where you only need to code up a few abstract methods.