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 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 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 {
public MyAdapter(int xlist, @Nullable List 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 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 list = tuijian.getList();
//填充新数据集合,发挥给V层
List 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 {
private IConteant.IView iView;
private IConteant.IModel model;
private WeakReference iViewWeakReference;
private WeakReference iModelWeakReference;
@Override
public void ShowData() {
model.zhanshi(new IConteant.IModel.OnCallBackLisenter() {
@Override
public void onCallBack(List 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 homeBeanList);
}
public interface IPresent{
public void ShowData();
public void attacthView(IView view);
public void detelecthView(IView view);
}
public interface IModel{
public interface OnCallBackLisenter{
public void onCallBack(List homeBeans);
}
public void zhanshi(OnCallBackLisenter onCallBackLisenter);
}
}
Fresco