GreenDao

GreenDao所用依赖:

https://github.com/greenrobot/greenDAO

Ok封装

package com.example.yan.zhoukaomoni1.data.ok;

import android.util.Log;

import java.io.IOException;


import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Ok {

    static  Ok ok;
    OkHttpClient okHttpClient;

    public Ok() {
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor())
                .build();
    }

    public static Ok getInstance() {
        if (null == ok) {
            synchronized (Ok.class) {
                if (null == ok) {
                    ok = new Ok();
                }
            }
        }
        return ok;
    }

    public void get(String urlString, Callback callback){
        Request request = new Request.Builder().url(urlString).build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    class LoggingInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            String method = request.method();
            Log.d("LoggingInterceptor", method);
            Response response = chain.proceed(request);
            return response;
        }
    }

}

App

package com.example.yan.zhoukaomoni1.ui;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.example.yan.zhoukaomoni1.data.DaoMaster;
import com.example.yan.zhoukaomoni1.data.DaoSession;
import com.facebook.drawee.backends.pipeline.Fresco;

public class App extends Application {

    private static App app;
    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        app = App.this;
        
        //数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"yanhengcun", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();

        Fresco.initialize(this);


    }

    public static App getInstance() {
        return app;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}

MainAcitvity

package com.example.yan.zhoukaomoni1;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.yan.zhoukaomoni1.data.HomeBean;
import com.example.yan.zhoukaomoni1.data.mvp.Presenter;
import com.example.yan.zhoukaomoni1.di.IConteant;
import com.facebook.drawee.view.SimpleDraweeView;

import java.net.URI;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IConteant.IView {

    private LinearLayout liner;
    private RecyclerView recycler;
    private IConteant.IPresent<IConteant.IView> present;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        present = new Presenter();
        present.attacthView(this);
        present.ShowData();
    }

    private void initView() {
        liner = (LinearLayout) findViewById(R.id.liner);
        recycler = (RecyclerView) findViewById(R.id.recycler);
    }

    @Override
    public void ShowDatas(final List<HomeBean> homeBeanList) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
                recycler.setLayoutManager(gridLayoutManager);
                MyAdapter myAdapter = new MyAdapter(R.layout.xlist,homeBeanList);
                recycler.setAdapter(myAdapter);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        present.detelecthView(this);
    }

    public class MyAdapter extends BaseQuickAdapter<HomeBean, BaseViewHolder> {

        public MyAdapter(int xlist, @Nullable List<HomeBean> data) {
            super(xlist,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, HomeBean item) {
            helper.setText(R.id.text,item.getTitle());
            String split = item.getImagurl().split("\\|")[0];
            Log.d(TAG, "convert: "+split);
            ((SimpleDraweeView)helper.getView(R.id.images)).setImageURI(Uri.parse(split));
           // SimpleDraweeView images =(SimpleDraweeView) helper.getView(R.id.images);
            //Uri parse = Uri.parse(item.getImagurl());
           // images.setImageURI(parse);
        }
    }
}

数据库Bean

package com.example.yan.zhoukaomoni1.data;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;

import java.io.Serializable;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class HomeBean implements Serializable{
    
    private static final long serialVersionUID = 1L;
    @Id(autoincrement = true)
    Long id;
    @Property
    String imagurl;
    @Property
    String title;
    @Generated(hash = 884409396)
    public HomeBean(Long id, String imagurl, String title) {
        this.id = id;
        this.imagurl = imagurl;
        this.title = title;
    }
    @Generated(hash = 931577662)
    public HomeBean() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getImagurl() {
        return this.imagurl;
    }
    public void setImagurl(String imagurl) {
        this.imagurl = imagurl;
    }
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }


}

Model层

package com.example.yan.zhoukaomoni1.data.mvp;

import android.content.Context;

import com.example.yan.zhoukaomoni1.Content;
import com.example.yan.zhoukaomoni1.data.HomeBean;
import com.example.yan.zhoukaomoni1.data.HomeBeanDao;
import com.example.yan.zhoukaomoni1.data.UitlsBean;
import com.example.yan.zhoukaomoni1.data.ok.Ok;
import com.example.yan.zhoukaomoni1.di.IConteant;
import com.example.yan.zhoukaomoni1.ui.App;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Model implements IConteant.IModel{
        String url = Content.URL_GG;
    @Override
    public void zhanshi(final OnCallBackLisenter onCallBackLisenter) {

        final HomeBeanDao homeBeanDao = App.getInstance().getDaoSession().getHomeBeanDao();

        final List<HomeBean> homeBeans = homeBeanDao.loadAll();
        if (homeBeans.size() > 0){

            onCallBackLisenter.onCallBack(homeBeans);
            return;
        }
        Ok.getInstance().get(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                onCallBackLisenter.onCallBack(null);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseing = response.body().string();
                Gson gson = new Gson();
                UitlsBean uitlsBean = gson.fromJson(responseing, UitlsBean.class);
                UitlsBean.TuijianBean tuijian = uitlsBean.getTuijian();
                List<UitlsBean.TuijianBean.ListBean> list = tuijian.getList();

                //填充新数据集合,发挥给V层
                List<HomeBean> homeBeans1 = new ArrayList<>();
                for (int i = 0; i < list.size();i++){
                    String image = list.get(i).getImages();
                    String title = list.get(i).getTitle();
                    HomeBean homeBean = new HomeBean();
                    homeBean.setImagurl(image);
                    homeBean.setTitle(title);
                    homeBeans1.add(homeBean);
                }
                onCallBackLisenter.onCallBack(homeBeans1);
                //③把数据存数据库中
                homeBeanDao.insertInTx(homeBeans1);
            }
        });
    }
}

Presenter

package com.example.yan.zhoukaomoni1.data.mvp;

import com.example.yan.zhoukaomoni1.data.HomeBean;
import com.example.yan.zhoukaomoni1.di.IConteant;

import java.lang.ref.WeakReference;
import java.util.List;

public class Presenter implements IConteant.IPresent<IConteant.IView> {

    private IConteant.IView iView;
    private IConteant.IModel model;
    private WeakReference<IConteant.IView> iViewWeakReference;
    private WeakReference<IConteant.IModel> iModelWeakReference;

    @Override
    public void ShowData() {

        model.zhanshi(new IConteant.IModel.OnCallBackLisenter() {
            @Override
            public void onCallBack(List<HomeBean> homeBeans) {
                iView.ShowDatas(homeBeans);
            }
        });

    }

    @Override
    public void attacthView(IConteant.IView iView) {
        this.iView = iView;
        this.model = new Model();
        iViewWeakReference = new WeakReference<>(iView);
        iModelWeakReference = new WeakReference<>(model);


    }

    @Override
    public void detelecthView(final IConteant.IView iView) {

        iModelWeakReference.clear();
        iViewWeakReference.clear();

    }
}

ICantent

package com.example.yan.zhoukaomoni1.di;

import com.example.yan.zhoukaomoni1.data.HomeBean;

import java.util.List;

public interface IConteant {

    public interface IView{

        public void ShowDatas(List<HomeBean> homeBeanList);
    }

    public interface IPresent<IView>{

        public void ShowData();

        public void attacthView(IView view);

        public void detelecthView(IView view);
    }

    public interface IModel{
        public interface OnCallBackLisenter{

            public void onCallBack(List<HomeBean> homeBeans);
        }
        public void zhanshi(OnCallBackLisenter onCallBackLisenter);
    }
}

Fresco 

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/images"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@mipmap/ic_launcher"
    />
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值