MVP实现列表

来源:互联网 发布:微信下单系统源码 编辑:程序博客网 时间:2024/06/11 01:02

Model层:public class ShowModelImpl implements ShowModel {    @Override   public void getData( final OnFinishListener listener) {          String url ="http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0";        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();        asyncHttpClient.get(url, new TextHttpResponseHandler() {            @Override            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {            }            @Override            public void onSuccess(int statusCode, Header[] headers, String responseString) {             //直接加高到主线程                Gson gson = new Gson();                News news = gson.fromJson(responseString, News.class);                //接口回调                if (listener!=null){                    listener.onSuccess(news);                }            }        });        /*//构建OK对象        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();        //构建Request        final Request request = new Request.Builder().url(url).build();        //得到Call        Call call = okHttpClient.newCall(request);        //执行异步        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {            //直接回调到子线程                String json = response.body().string();                Gson gson = new Gson();                News news = gson.fromJson(json, News.class);                //接口回调    if (listener!=null){        listener.onSuccess(news);    }            }        });*/    }}//M层的接口public interface ShowModel {    void getData(OnFinishListener listener);}//presenter层public class ShowPresenterImpl implements ShowPresenter,OnFinishListener {    ShowView showView;    private final ShowModel showModel;  //初始化    public ShowPresenterImpl(ShowView showView){        this.showView  = showView;        //多态        showModel = new ShowModelImpl();    }    @Override    public void relevance() {        //p跟m关联        showModel.getData(this);    }    @Override    public void onSuccess(News news) {        //关联view     showView.showData(news);    }}//p层接口public interface ShowPresenter {    void relevance();}//view层public class MainActivity extends AppCompatActivity implements ShowView{ private  String url ="http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0";    private RecyclerView rlr;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //找控件        rlr = (RecyclerView) findViewById(R.id.rlv);        //设置成布局管理器        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);        rlr.setLayoutManager(linearLayoutManager);        //p关联v        ShowPresenterImpl presenter = new ShowPresenterImpl(this);        //p关联m 做网络请求        presenter.relevance();    }    @Override    public void showData(News news) {        MyAdapter adapter = new MyAdapter(this,news);        rlr.setAdapter(adapter);    }}//v层的接口public interface ShowView {    void showData(News news);}//OnFinishListener接口public interface OnFinishListener {    void onSuccess(News news);}


原创粉丝点击