Task: Get data from the network and then display it on the mainactivity listview
I. Loading the framework needed
1. MVP Framework
Compile ' com.hannesdorfmann.mosby:mvp:2.0.1 '
Compile ' com.hannesdorfmann.mosby:viewstate:2.0.1 '
2, retrofit and Rxjava framework
Compile ' io.reactivex:rxandroid:1.2.0 '
Compile ' io.reactivex:rxjava:1.1.5 '
Compile ' com.squareup.okhttp:okhttp:2.5.0 '
Compile ' com.squareup.retrofit:retrofit:2.0.0-beta1 '
Compile ' com.squareup.retrofit:converter-gson:2.0.0-beta1 '
Compile ' com.squareup.retrofit:adapter-rxjava:2.0.0-beta1 '
Second, design sketches
According to the MVP design pattern:
The network request that provides the data is placed in the model package.
The class that will display the screen data into the view package
The logic for processing data and transformations is put into the presents package.
We are here: only need to implement when the data is obtained, the ListView display situation is ready.
Iii. Commencement of work
1, create retrofit network request retrofit use to put under the model this package
Public classarticleentity{//converting JSON data to entity classes PrivateString title; PrivateString Detail; PublicString Getdetail () {returndetail; } Public voidSetdetail (String detail) { This. Detail =detail; } PublicString GetTitle () {returntitle; } Public voidSettitle (String title) { This. title =title; }}
articleentity
Public Interface Dataapi {// send data request @GET ("Refreshlayout/api/newdata{pagenumber}.json") Observableint pagenumber);}
Dataapi
Finally: Create
Public classhttpconnection {//address: From the Internet search Private Static FinalString base_url = "http://7xk9dj.com1.z0.glb.clouddn.com/"; Private Statichttpconnection mhttconnection; PrivateRetrofit Mretrofit; //Create Retrofit Privatehttpconnection () {Mretrofit=NewRetrofit.builder (). Addcalladapterfactory (Rxjavacalladapterfactory.create ()). Addconver Terfactory (Gsonconverterfactory.create ()). BASEURL (Base_url). build (); } //using the singleton mode, plus double lock Public Static synchronizedhttpconnection Newinstace () {if(Mhttconnection = =NULL){ synchronized(Httpconnection.class) {mhttconnection=Newhttpconnection (); } } returnmhttconnection; } //method of getting data, subscriber as callback interface Public voidGetData (intpagenum,subscriber Subscriber) {DATAAPI API= Mretrofit.create (Dataapi.class); Observable Observable=api.getarticles (pagenum); Observable.subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscrib E (subscriber); }}
httpconnection
2. Create View
Analysis: According to our needs, we only need the screen to display the data this one action, so we designed the view is like this
// implementation: Creating interfaces and inheriting interfaces Mvpview Public Interface extends mvpview{ // get to data, show void Show (arraylist<articleentity> Articleitem);}
iarticle
3, Design present class
Again, we just need to show the method.
Public classArticlepresentextendsMvpbasepresenter<iarticleview> { Private intMviewpage = 0; Privatehttpconnection mconnection; @Override Public voidAttachview (Iarticleview view) {Super. Attachview (view); Mconnection=Httpconnection.newinstace (); } //get the data, process it, and pass it to the view's broker Public voidShow () {//get the view of the previous step FinalIarticleview view =GetView (); if(View! =NULL) {Subscriber subscriber=NewSubscriber () {@Override Public voidoncompleted () {LOG.D ("Mainactivity", "Done"); } @Override Public voidOnError (Throwable e) {} @Override Public voidonNext (Object o) {//when the data is fetched, the method that calls the viewView.show ((arraylist<articleentity>) o); } }; //Get Related DataMconnection.getdata (Mviewpage,subscriber); } }}
articlepresnt
4. mainactivity Display Data
Basemvpactivity inherited mvpbaseactivity designed a basic encapsulation method, encapsulating some basic methods,
Generics: A generic that is created after the class name is created before the return value of the method
Public Abstract classBasemvpactivity <vextendsMvpview,textendsMvppresenter<v>>extendsMvpactivity<v,t>{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Initview (savedinstancestate); Setlistener (); } Public Abstract voidInitview (Bundle savedinstancestate); Public Abstract voidSetlistener (); Public<vtextendsView> VT Getviewbyid (intID) { return(VT) Findviewbyid (ID); }}
basemvpactivity
Public classMainactivityextendsBasemvpactivity<iarticleview,articlepresent>Implementsiarticleview{PrivateListView Mlistview; PrivateArticleadapter Marticleadapter; @NonNull @Override Publicarticlepresent Createpresenter () {return Newarticlepresent (); } @Override Public voidInitview (Bundle savedinstancestate) {Mlistview=Getviewbyid (R.id.main_listview); Marticleadapter=NewArticleadapter ( This,NewArraylist<articleentity>()); Mlistview.setadapter (Marticleadapter); } @Override Public voidSetlistener () {} @Override Public voidShow (arraylist<articleentity>Articleitem) {marticleadapter.addarticles (Articleitem); }}
mainactivity
Step: Inherit basemvpactivity, inherit the Iacrticle interface just now
Design method: Load the class that gets the data into the model, and then specify the interface method for the view display (for example, what interface is displayed when there is no data, what interface is displayed when there is data), and the data in it is not the same.
The logic for processing data and invoking the view method on different data is done in present.
A simple MVP design