日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MVP结合(RecycleView,Retorfit,GreenDao和EventBus)数据展示

發布時間:2024/3/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVP结合(RecycleView,Retorfit,GreenDao和EventBus)数据展示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼使用的依賴:

compile'org.greenrobot:greendao:3.0.1'compile'org.greenrobot:greendao-generator:3.0.0'compile 'com.jakewharton:butterknife:7.0.0'compile 'org.greenrobot:eventbus:3.1.1'compile 'com.facebook.fresco:fresco:0.14.1'compile 'com.squareup.okhttp3:mockwebserver:3.9.0'compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'


GreenDao配置文件:


依賴以及其它 compile'org.greenrobot:greendao:3.0.1'compile'org.greenrobot:greendao-generator:3.0.0'greendao {schemaVersion 1daoPackage 'com.bwie.greendao.gen'targetGenDir 'src/main/java'}apply plugin: 'org.greenrobot.greendao'classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'


頁面布局:

activity_main

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.muhanxi.myapplication.MainActivity"><android.support.v7.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/recycleview"/></LinearLayout>


item_layout:


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="horizontal"android:layout_height="wrap_content"><com.facebook.drawee.view.SimpleDraweeViewandroid:layout_width="100dp"android:layout_height="100dp"android:id="@+id/adapter_simpledraweeview"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/adapter_textview"/></LinearLayout>

activity_main2:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.muhanxi.myapplication.Main2Activity"></LinearLayout>

主要代碼:

MainActivity:


package com.example.muhanxi.myapplication;import android.app.Activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView;import com.example.muhanxi.myapplication.adapter.IApdater; import com.example.muhanxi.myapplication.presenter.MainPresenter; import com.example.muhanxi.myapplication.view.IMainView;import java.util.List;import butterknife.BindView; import butterknife.ButterKnife;public class MainActivity extends Activity implements IMainView {@BindView(R.id.recycleview)RecyclerView recycleview;private MainPresenter presenter;private IApdater adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);presenter = new MainPresenter(this);presenter.get();LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);adapter = new IApdater(this);recycleview.setLayoutManager(manager);recycleview.setAdapter(adapter);List<ListBean> listBeans = IApplication.session.getListBeanDao().loadAll();for(ListBean bean : listBeans){System.out.println(bean.toString());}}@Overridepublic void onSuccess(Bean bean) {adapter.addData(bean);}@Overridepublic void onFailure(Exception e) {} }


初始化IApplication類:

package com.example.muhanxi.myapplication;import android.app.Application;import com.example.muhanxi.myapplication.dao.DaoMaster; import com.example.muhanxi.myapplication.dao.DaoSession; import com.facebook.drawee.backends.pipeline.Fresco;import org.greenrobot.greendao.database.Database;import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory;/*** Created by muhanxi on 17/12/1.*/public class IApplication extends Application {public static IGetDataBase iGetDataBase;public static DaoSession session;@Overridepublic void onCreate() {super.onCreate();Fresco.initialize(this);Retrofit retrofit = new Retrofit.Builder().baseUrl("http://v.juhe.cn").addConverterFactory(GsonConverterFactory.create()).build();iGetDataBase = retrofit.create(IGetDataBase.class);DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"test");Database database = helper.getWritableDb();session = new DaoMaster(database).newSession();} }


Main2Activity:

package com.example.muhanxi.myapplication;import android.app.Activity; import android.os.Bundle; import android.widget.Toast;import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe;public class Main2Activity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);EventBus.getDefault().register(this);}@Subscribe(sticky = true)public void event(EventBean eventBean){Toast.makeText(this, ""+ eventBean.getUrl() + " " + eventBean.getTitle(), Toast.LENGTH_SHORT).show();}@Overrideprotected void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this);} }


生成接口從其中獲得get和post請求:

IGetDataBase:

package com.example.muhanxi.myapplication;import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query;/*** Created by muhanxi on 17/12/1.*/public interface IGetDataBase {/*** get 請求* @param key* @return*/@GET("/weixin/query")Call<Bean> get(@Query("key") String key);/*** post 請求* @param key* @return*/@FormUrlEncoded@POST("/weixin/query")Call<Bean> post(@Field("key") String key);}

EventBus設置Bean類:

EventBean:

package com.example.muhanxi.myapplication;/*** Created by muhanxi on 17/12/1.*/public class EventBean {public String url;public String title;public EventBean(String url, String title) {this.url = url;this.title = title;}public String getUrl() {return url;}public String getTitle() {return title;} }


Bean:



package com.example.muhanxi.myapplication;import java.util.List;/*** Created by muhanxi on 17/12/1.*/public class Bean {/*** reason : 請求成功* result : {"list":[{"id":"wechat_20171201008724","title":"威廉·德雷謝維奇《簡·奧斯丁的教導》:改變生活的藝術","source":"文藝報1949","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604411.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201008724"},{"id":"wechat_20171201010058","title":"收藏!2018年放假安排來啦!","source":"環球時報","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604760.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201010058"},{"id":"wechat_20171201010006","title":"收藏!2018年放假安排來啦!","source":"環球時報","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604760.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201010006"},{"id":"wechat_20171201009971","title":"有這些特質的人,不容易變老","source":"中國茶訊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-34528250.static/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201009971"},{"id":"wechat_20171201011040","title":"擇一人深愛,等一人終老","source":"勵志愛情語錄","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60403353.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201011040"},{"id":"wechat_20171201007632","title":"張翰鄭爽終于在一起了,網友不經意吐槽心疼娜扎!","source":"八卦姐嘴太毒","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60603433.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007632"},{"id":"wechat_20171201007157","title":"45歲寧靜穿成這樣尷尬全場,網友:都掉地上了!看了心都寧靜不了!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602673.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007157"},{"id":"wechat_20171201007148","title":"最讓人臉紅的穿幫鏡頭,黃海波抱起圓圓露出了什么,王寶強馬蓉躺槍","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602769.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007148"},{"id":"wechat_20171201007959","title":"王爾德:書無所謂道德或不道德丨單讀","source":"單讀","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-37683214.static/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007959"},{"id":"wechat_20171201007906","title":"王爾德:書無所謂道德或不道德丨單讀","source":"單讀","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-37683214.static/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007906"},{"id":"wechat_20171201006966","title":"45歲寧靜穿成這樣尷尬全場,網友:都掉地上了!看了心都寧靜不了!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602673.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201006966"},{"id":"wechat_20171201007147","title":"第一港姐,6段戀情5次小三上位,被騙財騙色今與81歲父親相依為命!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602637.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007147"},{"id":"wechat_20171201007171","title":"林志玲最尷尬的一次,穿錯裙子動作太明顯,女神形象全無!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602677.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007171"},{"id":"wechat_20171201007254","title":"不追月的彩云,緣何烏云密布","source":"紅樓夢學刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-34106286.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007254"},{"id":"wechat_20171201004798","title":"不追月的彩云,緣何烏云密布","source":"紅樓夢學刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-34106286.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004798"},{"id":"wechat_20171201004763","title":"東方早安丨大風起兮云飛揚","source":"瞭望東方周刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-40470651.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004763"},{"id":"wechat_20171201004719","title":"東方早安丨大風起兮云飛揚","source":"瞭望東方周刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-40470651.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004719"},{"id":"wechat_20171201007095","title":"《班迪故事口袋》\u2014\u2014收服豬八戒","source":"班迪俱樂部","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-56298014.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007095"},{"id":"wechat_20171201004764","title":"詩詞丨一扇小軒窗,望斷多少柔腸","source":"知秋堂經典美文","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60601116.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004764"},{"id":"wechat_20171201005887","title":"五十歲時,你會明白人一生干不了幾件事情","source":"慈懷讀書會","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60601931.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201005887"}],"totalPage":10461,"ps":20,"pno":1}* error_code : 0*/private String reason;private ResultBean result;private int error_code;public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}public ResultBean getResult() {return result;}public void setResult(ResultBean result) {this.result = result;}public int getError_code() {return error_code;}public void setError_code(int error_code) {this.error_code = error_code;}public static class ResultBean {/*** list : [{"id":"wechat_20171201008724","title":"威廉·德雷謝維奇《簡·奧斯丁的教導》:改變生活的藝術","source":"文藝報1949","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604411.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201008724"},{"id":"wechat_20171201010058","title":"收藏!2018年放假安排來啦!","source":"環球時報","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604760.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201010058"},{"id":"wechat_20171201010006","title":"收藏!2018年放假安排來啦!","source":"環球時報","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604760.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201010006"},{"id":"wechat_20171201009971","title":"有這些特質的人,不容易變老","source":"中國茶訊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-34528250.static/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201009971"},{"id":"wechat_20171201011040","title":"擇一人深愛,等一人終老","source":"勵志愛情語錄","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60403353.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201011040"},{"id":"wechat_20171201007632","title":"張翰鄭爽終于在一起了,網友不經意吐槽心疼娜扎!","source":"八卦姐嘴太毒","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60603433.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007632"},{"id":"wechat_20171201007157","title":"45歲寧靜穿成這樣尷尬全場,網友:都掉地上了!看了心都寧靜不了!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602673.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007157"},{"id":"wechat_20171201007148","title":"最讓人臉紅的穿幫鏡頭,黃海波抱起圓圓露出了什么,王寶強馬蓉躺槍","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602769.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007148"},{"id":"wechat_20171201007959","title":"王爾德:書無所謂道德或不道德丨單讀","source":"單讀","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-37683214.static/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007959"},{"id":"wechat_20171201007906","title":"王爾德:書無所謂道德或不道德丨單讀","source":"單讀","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-37683214.static/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007906"},{"id":"wechat_20171201006966","title":"45歲寧靜穿成這樣尷尬全場,網友:都掉地上了!看了心都寧靜不了!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602673.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201006966"},{"id":"wechat_20171201007147","title":"第一港姐,6段戀情5次小三上位,被騙財騙色今與81歲父親相依為命!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602637.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007147"},{"id":"wechat_20171201007171","title":"林志玲最尷尬的一次,穿錯裙子動作太明顯,女神形象全無!","source":"娛樂圈那點事","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60602677.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007171"},{"id":"wechat_20171201007254","title":"不追月的彩云,緣何烏云密布","source":"紅樓夢學刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-34106286.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007254"},{"id":"wechat_20171201004798","title":"不追月的彩云,緣何烏云密布","source":"紅樓夢學刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-34106286.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004798"},{"id":"wechat_20171201004763","title":"東方早安丨大風起兮云飛揚","source":"瞭望東方周刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-40470651.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004763"},{"id":"wechat_20171201004719","title":"東方早安丨大風起兮云飛揚","source":"瞭望東方周刊","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-40470651.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004719"},{"id":"wechat_20171201007095","title":"《班迪故事口袋》\u2014\u2014收服豬八戒","source":"班迪俱樂部","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-56298014.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201007095"},{"id":"wechat_20171201004764","title":"詩詞丨一扇小軒窗,望斷多少柔腸","source":"知秋堂經典美文","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60601116.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201004764"},{"id":"wechat_20171201005887","title":"五十歲時,你會明白人一生干不了幾件事情","source":"慈懷讀書會","firstImg":"http://zxpic.gtimg.com/infonew/0/wechat_pics_-60601931.jpg/640","mark":"","url":"http://v.juhe.cn/weixin/redirect?wid=wechat_20171201005887"}]* totalPage : 10461* ps : 20* pno : 1*/private int totalPage;private int ps;private int pno;private List<ListBean> list;public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getPs() {return ps;}public void setPs(int ps) {this.ps = ps;}public int getPno() {return pno;}public void setPno(int pno) {this.pno = pno;}public List<ListBean> getList() {return list;}public void setList(List<ListBean> list) {this.list = list;}} }

ListBean:

package com.example.muhanxi.myapplication;import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Generated;@Entity public class ListBean {/*** id : wechat_20171201008724* title : 威廉·德雷謝維奇《簡·奧斯丁的教導》:改變生活的藝術* source : 文藝報1949* firstImg : http://zxpic.gtimg.com/infonew/0/wechat_pics_-60604411.jpg/640* mark :* url : http://v.juhe.cn/weixin/redirect?wid=wechat_20171201008724*/@Id(autoincrement = true)private Long iid;private String id;private String title;private String source;private String firstImg;private String mark;private String url;@Generated(hash = 2061991154)public ListBean(Long iid, String id, String title, String source, String firstImg,String mark, String url) {this.iid = iid;this.id = id;this.title = title;this.source = source;this.firstImg = firstImg;this.mark = mark;this.url = url;}@Generated(hash = 777734033)public ListBean() {}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getSource() {return source;}public void setSource(String source) {this.source = source;}public String getFirstImg() {return firstImg;}public void setFirstImg(String firstImg) {this.firstImg = firstImg;}public String getMark() {return mark;}public void setMark(String mark) {this.mark = mark;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public Long getIid() {return this.iid;}public void setIid(Long iid) {this.iid = iid;}@Overridepublic String toString() {return "ListBean{" +"iid=" + iid +", id='" + id + '\'' +", title='" + title + '\'' +", source='" + source + '\'' +", firstImg='" + firstImg + '\'' +", mark='" + mark + '\'' +", url='" + url + '\'' +'}';} }


放置獲取的數據的Bean類,其中ListBean用于生成GreenDao中dao包的三大類:其中有Bean類和ListBean:





其中ListBean的注意點:@Entity和@Id是必須要寫的:



MVP三層的使用:


M層的model包中的三個類:

MainModelImpl:

package com.example.muhanxi.myapplication.model;import com.example.muhanxi.myapplication.Bean; import com.example.muhanxi.myapplication.IApplication;import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;/*** Created by muhanxi on 17/12/1.*/public class MainModelImpl {/*** get 請求* @param callBack*/public void getData(final ModelCallBack callBack){Call<Bean> call = IApplication.iGetDataBase.get("88f7bbc507e3ecacfaeab2b47dd8936f");call.enqueue(new Callback<Bean>() {@Overridepublic void onResponse(Call<Bean> call, Response<Bean> response) {Bean bean = response.body();callBack.onSuccess(bean);// 保存到數據庫IApplication.session.getListBeanDao().insertInTx(bean.getResult().getList());}@Overridepublic void onFailure(Call<Bean> call, Throwable t) {callBack.onFailure(new Exception(""));}});}/*** post 請求*/public void postData(final ModelCallBack callBack){Call<Bean> call = IApplication.iGetDataBase.post("88f7bbc507e3ecacfaeab2b47dd8936f");call.enqueue(new Callback<Bean>() {@Overridepublic void onResponse(Call<Bean> call, Response<Bean> response) {Bean bean = response.body() ;callBack.onSuccess(bean);}@Overridepublic void onFailure(Call<Bean> call, Throwable t) {callBack.onFailure(new Exception(""));}});}}


ModelCallBack:

package com.example.muhanxi.myapplication.model;import com.example.muhanxi.myapplication.Bean;/*** Created by muhanxi on 17/12/1.*/public interface ModelCallBack {public void onSuccess(Bean bean);public void onFailure(Exception e);}

MainModel:

package com.example.muhanxi.myapplication.model;/*** Created by muhanxi on 17/12/1.*/public interface MainModel { }


V層View包的接口類:

MAinView:

package com.example.muhanxi.myapplication.view;import com.example.muhanxi.myapplication.Bean;/*** Created by muhanxi on 17/12/1.*/public interface IMainView {public void onSuccess(Bean bean);public void onFailure(Exception e);}


P層的Presenter包下的中轉類:用于連接m層和v層:

MainPresenter:

package com.example.muhanxi.myapplication.presenter;import com.example.muhanxi.myapplication.Bean; import com.example.muhanxi.myapplication.model.MainModelImpl; import com.example.muhanxi.myapplication.model.ModelCallBack; import com.example.muhanxi.myapplication.view.IMainView;/*** Created by muhanxi on 17/12/1.*/public class MainPresenter {private IMainView iView ;private MainModelImpl mainModel ;public MainPresenter(IMainView view){this.iView = view;this.mainModel = new MainModelImpl();}public void get(){mainModel.getData(new ModelCallBack() {@Overridepublic void onSuccess(Bean bean) {if(iView != null){iView.onSuccess(bean);}}@Overridepublic void onFailure(Exception e) {if(iView != null){iView.onFailure(e);}}});}public void post(){mainModel.postData(new ModelCallBack() {@Overridepublic void onSuccess(Bean bean) {if(iView != null){iView.onSuccess(bean);}}@Overridepublic void onFailure(Exception e) {if(iView != null){iView.onFailure(e);}}});}}
GreenDao中生成的三個主要類:點擊軟件里的一個“ 錘子”類的按鈕就可以出來,dao類的使用在“http://blog.csdn.net/oaitan/article/details/51464743”

這是使用的過程:可以看看:



最后RecycleView的Adapter類的代碼:

IAdapter:

package com.example.muhanxi.myapplication.adapter;import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;import com.example.muhanxi.myapplication.Bean; import com.example.muhanxi.myapplication.EventBean; import com.example.muhanxi.myapplication.ListBean; import com.example.muhanxi.myapplication.Main2Activity; import com.example.muhanxi.myapplication.R; import com.facebook.drawee.view.SimpleDraweeView;import org.greenrobot.eventbus.EventBus;import java.util.ArrayList; import java.util.List;import butterknife.BindView; import butterknife.ButterKnife;/*** Created by muhanxi on 17/12/1.*/public class IApdater extends RecyclerView.Adapter<IApdater.IViewHolder> {Context context;List<ListBean> list ;public IApdater(Context context) {this.context = context;}public void addData(Bean bean){if(list == null){list = new ArrayList<>();}list.addAll(bean.getResult().getList());notifyDataSetChanged();}@Overridepublic IApdater.IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = View.inflate(context, R.layout.layout, null);return new IViewHolder(view);}@Overridepublic void onBindViewHolder(IApdater.IViewHolder holder, final int position) {holder.adapterSimpledraweeview.setImageURI(list.get(position).getFirstImg());holder.adapterTextview.setText(list.get(position).getTitle());holder.adapterTextview.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {EventBus.getDefault().postSticky(new EventBean(list.get(position).getFirstImg(),list.get(position).getTitle()));context.startActivity(new Intent(context, Main2Activity.class));}});}@Overridepublic int getItemCount() {return list == null ? 0 : list.size();}static class IViewHolder extends RecyclerView.ViewHolder{@BindView(R.id.adapter_simpledraweeview)SimpleDraweeView adapterSimpledraweeview;@BindView(R.id.adapter_textview)TextView adapterTextview;IViewHolder(View view) {super(view);ButterKnife.bind(this, view);}} }

最后整個小Demo就結束了,可以簡單參考參考:

對了最后還有GreenDao的配置類:




效果如下:


配置一定不要忘了,否則就

出不來了






總結

以上是生活随笔為你收集整理的MVP结合(RecycleView,Retorfit,GreenDao和EventBus)数据展示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。