MVP基类使用介绍
来源:互联网 发布:windows rt刷安卓 编辑:程序博客网 时间:2024/06/04 01:13
public class BasePresenter<T extends IMvpView> { //IMVpView为总接口,里面没有任何东西 private T t; public void SetAttachView(T t) { this.t = t; } public T getView() { return t; }}
//httputils类,工具类的使用,避免了只能解析单一对象,想要什么类型的对象,就传入哪个Bean类
public class httputils { public static <T> void GetDataFormService(String url, final CallBackToPresenter callBackToPresenter, final Class<T> t) { RequestParams params = new RequestParams(); params.setUri(url); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); T t1 = gson.fromJson(result, t); callBackToPresenter.ToPresenter(t1); } @Override public void onError(Throwable ex, boolean isOnCallback) { Log.e("Error", "onError: " + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } public interface CallBackToPresenter<T> { void ToPresenter(T t); }}
public class HomePresenter extends BasePresenter<IHomeView> {//presenter层,作用:从httputils类中拿到数据,并回调回view层 String url = "http://result.eolinker.com/gfGTLlHc049c6b450500b16971f52bd8e83f6b2fed305ab?uri=news";//需要请求的url地址 public <T> void getDataToView(final Class<T> t) { httputils.GetDataFormService(url, new httputils.CallBackToPresenter<T>() { @Override public void ToPresenter(T t1) { getView().CallBack(t1);//把model层传回来的数据再次传递到view层,由view层做显示处理 } }, t); }}
private void GetDate() {//在activity中的方法 HomePresenter presenter = new HomePresenter(); //首先要拿到HomePresenter 也就是Presenter层的引用 presenter.SetAttachView(this); //这个方法是在basepresenter中的方法,因为homePresenter继承了basePresenter,所以能用HomePresenter中的方法, // 并把当前acitivity的引用传过去,然后用接口回调,把数据传到acitivity层中进行显示 presenter.getDataToView(HomeBean.class); }//IMVpView是一个空接口
public interface IHomeView<T> extends IMvpView { //成功调用的方法 void CallBack(T t); //失败调用的方法 //void CallErrorBack(T t);}
阅读全文
0 0
- MVP基类使用介绍
- MVP基类介绍
- MVP介绍以及使用
- MVP介绍
- mosby mvp类库使用
- MVP使用
- Mvp使用
- MVP模式介绍
- MVP模式介绍:
- Android MVP 介绍
- MVP模式简单介绍
- MVP模式介绍
- Android MVP 介绍
- MVP模式 简单介绍
- MVP设计模式介绍
- MVP的简单介绍
- MVP框架介绍
- Android MVP 概述:MVP的组件介绍
- selenium3+python2.7启动Firefox 53提示:socket.error: [Errno 10054]
- Design Patterns
- thymeleaf模板
- Low Cost Air Travel UVALive
- STM32CUBE_MX_STM32F030C8T6_USART_TX应用
- MVP基类使用介绍
- Javascript 语言的模板引擎:Mustache
- io流的使用:
- IO流
- c/c++出现:fatal error LNK 1120:1 unresolved externals
- 异常处理:finally块中代码什么时候执行总结!
- win7怎么修改开机密码(最快) win7修改开机密码最便捷的方法
- 怎么学好php
- 流的作用