GreenDao的数据库存储

public class Fragment_rm extends Fragment implements Iview{

    @BindView(R.id.rv)
    RecyclerView rv;
    Unbinder unbinder;
    Persenter   pp;




    private  List<Bean.ResultsBean> list;
    private DaoMaster daoMaster;
    private DaoSession  daoSession;
    private UserDao  userDao;
    User   user;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(), R.layout.rm, null);

        //创建数据库数据库
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getContext(), "student.db", null);
        daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        daoSession = daoMaster.newSession();

        pp=new Persenter(getActivity(),this);
        pp.getData();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    //传递类的线程
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void eventBusReceive(SJ_wl sj_wl) {
        //进行赋值
        Toast.makeText(getActivity(),sj_wl.getSf().toString()+"",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

    @Override
    public void ShowView(Bean bean) {
        LinearLayoutManager   manager=new LinearLayoutManager(getActivity());
        rv.setLayoutManager(manager);
        rv.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));

        Myadapter   rvadapter=new Myadapter(getActivity(),bean);
        rv.setAdapter(rvadapter);


        //将数据存入greennDaod的数据库
        list=bean.getResults();
        userDao=daoSession.getUserDao();
        for (int i=0;i<bean.getResults().size();i++)
        {

           userDao.insert(new User((long) i,bean.getResults().get(i).getCreatedAt(),bean.getResults().get(i).getDesc()));

        }





    }

    //销毁 的方法
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值