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); } }
GreenDao的数据库存储
最新推荐文章于 2019-07-12 10:45:26 发布