greendao

1.添加依赖

在Model 下 bulid.gradle文件下的dependencies下添加所需依赖

compile 'org.greenrobot:greendao:3.2.2' // add library
compile 'org.greenrobot:greendao-generator:3.2.2'

这句话是在上边写的 小渣渣啊→_→

apply plugin: 'org.greenrobot.greendao'

2.在project bulid.gradle下进行配置

   buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'//greenDao
        }
    }

3.在Model 下 bulid.gradle文件下的android在对greendao的generator生成文件进行配置

greendao {
        schemaVersion 1  //版本
        daoPackage '生成文件包名' // 一般为app包名+生成文件的文件夹名
        targetGenDir 'src/main/java' //生成文件路径
    }

例如:
在这里插入图片描述4.创建实体类
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = “NAME”)
private String name;

注意:编写完实体类以后在实体类界面下按下Ctrl+F9(Make project),程序会自动编译生成dao文件,生成的文件一共有三个。
5.使用Greendao
(1)创建一个application类,在application中完成DaoSession的初始化,避免以后重复初始化,便于使用。

package com.example.shopcrats.app;

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

import com.example.shopcrats.NetWrokProwor;
import com.example.shopcrats.greenbean.DaoMaster;
import com.example.shopcrats.greenbean.DaoSession;
import com.facebook.drawee.backends.pipeline.Fresco;


public class MyApplication extends Application {

    private static MyApplication App;
    private DaoMaster.DevOpenHelper data;
    private SQLiteDatabase writableDatabase;
    private DaoMaster daoMaster;
    private DaoSession daoSession;


    @Override
    public void onCreate() {
        super.onCreate();
        App = this;
        Fresco.initialize(this);
        NetWrokProwor.getNetWrokProwor(this);
        setDataBase();
    }

    public static MyApplication getInstance(){
        return App;
    }

    private void setDataBase() {
        //通过DaoMaster的内部类DevOpenHelper
        data = new DaoMaster.DevOpenHelper(this, "data", null);
        writableDatabase = data.getWritableDatabase();
        daoMaster = new DaoMaster(writableDatabase);
        daoSession = daoMaster.newSession();
    }


    public SQLiteDatabase getWritableDatabase() {
        return writableDatabase;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}

6.bean类(参数是自己要存入地方的参数)

package com.example.shopcrats;

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


@Entity
public class GreenDao {
   @Id(autoincrement = true)
   private Long id;
   private int CommentId;
   private String num;
   @Generated(hash = 1121101909)
   public GreenDao(Long id, int CommentId, String num) {
       this.id = id;
       this.CommentId = CommentId;
       this.num = num;
   }
   @Generated(hash = 766040118)
   public GreenDao() {
   }
   public Long getId() {
       return this.id;
   }
   public void setId(Long id) {
       this.id = id;
   }
   public int getCommentId() {
       return this.CommentId;
   }
   public void setCommentId(int CommentId) {
       this.CommentId = CommentId;
   }
   public String getNum() {
       return this.num;
   }
   public void setNum(String num) {
       this.num = num;
   }


}

7.主页面

package com.example.shopcrats;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopcrats.adapter.ExAdapter;
import com.example.shopcrats.app.MyApplication;
import com.example.shopcrats.bean.CountPrice;
import com.example.shopcrats.bean.GoodsBean;
import com.example.shopcrats.bean.MessageEvent;
import com.example.shopcrats.greenbean.GreenDaoDao;
import com.example.shopcrats.presenter.Presenter;
import com.example.shopcrats.view.View_MianActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View_MianActivity{

    private ExpandableListView MyExpandableListView;
    private CheckBox quanxuancheck;
    private TextView allprice;
    private Button jiesuan;
    private Presenter presenter;
    private ExAdapter exAdapter;
    private GreenDaoDao greenDaoDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        greenDaoDao = MyApplication.getInstance().getDaoSession().getGreenDaoDao();
        boolean netWrokProwor = NetWrokProwor.getNetWrokProwor(this);
        if(netWrokProwor){
            initData();
        }else{
            Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show();
        }


            if(!EventBus.getDefault().isRegistered(true)){
                EventBus.getDefault().register(this);
            }
    }


    private void initView() {
        MyExpandableListView = findViewById(R.id.MyExpandableListView);
        quanxuancheck = findViewById(R.id.quanxuancheck);
        allprice = findViewById(R.id.allprice);
        jiesuan = findViewById(R.id.jiesuan);


        quanxuancheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前条目状态
                boolean allcheckbox = exAdapter.isAllcheckbox();
                //给条目复制
                exAdapter.isAllCheckBox(!allcheckbox);
                //刷新
                exAdapter.notifyDataSetChanged();
            }
        });
    }


    private void initData() {
        presenter = new Presenter(this);
        Map<String, String> map = new HashMap<>();
        map.put("uid","71");
        presenter.request(map);
    }


    @Override
    public void onSuccess(List<GoodsBean.DataBean> data) {
        for(int i = 0 ; i < data.size() ; i++){
            GoodsBean.DataBean dataBean = data.get(i);
            List<GoodsBean.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0 ; j < list.size() ; j++){
                GoodsBean.DataBean.ListBean listBean = list.get(j);
                GreenDao greenDao = new GreenDao(null, listBean.getPscid(), listBean.getNum() + "");
                greenDaoDao.insert(greenDao);
            }
        }
        exAdapter = new ExAdapter(data,this);
        MyExpandableListView.setAdapter(exAdapter);
        List<GreenDao> greenDaos = greenDaoDao.loadAll();
        for (int i = 0 ; i < greenDaos.size() ; i++){
            GreenDao greenDao = greenDaos.get(i);
            int commentId = greenDao.getCommentId();
            String num = greenDao.getNum();
            Log.e("lsq","商品id:"+commentId+"----商品数量:"+num);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onMessagesEvent(CountPrice countPrice){
        allprice.setText(countPrice.getPrice()+"");
        jiesuan.setText("去结算("+countPrice.getCount()+")");
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onMessagesEvent(MessageEvent messageEvent){
        quanxuancheck.setChecked(messageEvent.isCheck());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(true)){
            EventBus.getDefault().unregister(this);
        }
        if(presenter != null){
            presenter = null;
        }
    }
}

发布了57 篇原创文章 · 获赞 12 · 访问量 8309
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览