public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment1, null);
//获取资源ID
recyclerView = view.findViewById(R.id.recycleview);
allprice = view.findViewById(R.id.allprice);
quanxuan = view.findViewById(R.id.quanxuan);
quanxuan.setOnClickListener(this);
//得到p层
presenter = new Presenter();
presenter.attch(this);
presenter.getModel(url);
//布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
//通过表名,拿到或者创建表
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getContext(), "testDB");
//拿到一个可读写的数据库对象
SQLiteDatabase db = helper.getWritableDatabase();
//拿到greendao的master对象
DaoMaster daoMaster = new DaoMaster(db);
//拿到session对象
DaoSession daoSession = daoMaster.newSession();
resuBeanDao = daoSession.getResuBeanDao();
//判断网络
networkConnected = NetWork.isNetworkConnected(getContext());
if (!networkConnected){
List<ResuBean> list = resuBeanDao.queryBuilder().build().list();
MyAdapter myAdapter = new MyAdapter(list,getContext());
recyclerView.setAdapter(myAdapter);
}
return view;
}
@Override
public void Presenter(String data) {
if (networkConnected){
Gson gson = new Gson();
Resu resu = gson.fromJson(data, Resu.class);
myRecycleViewAdapter = new MyRecycleViewAdapter(resu,getContext());
recyclerView.setAdapter(myRecycleViewAdapter);
for (int i = 0; i < resu.getResult().size(); i++) {
ResuBean resuBean = new ResuBean();
resuBean.setCommodityId(resu.getResult().get(i).getCommodityId());
resuBean.setCommodityName(resu.getResult().get(i).getCommodityName());
resuBean.setCount(resu.getResult().get(i).getCount());
resuBean.setPic(resu.getResult().get(i).getPic());
resuBean.setPrice(resu.getResult().get(i).getPrice());
resuBeanDao.insertOrReplace(resuBean);
}
myRecycleViewAdapter.setListener(new MyRecycleViewAdapter.CallBack() {
@Override
public void callback(List<Resu.Result> list) {
double totalPrice = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isCheck){
String price = list.get(i).getPrice();
Double price1 = Double.valueOf(price);
String count = list.get(i).getCount();
int numm = Integer.parseInt(count);
totalPrice = totalPrice+price1*numm;
}
}
allprice.setText("合计:"+totalPrice);
}
});
}
}
//解绑
@Override
public void onDestroy() {
super.onDestroy();
presenter.detch();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.quanxuan:
checkSeller(quanxuan.isChecked());
myRecycleViewAdapter.notifyDataSetChanged();
break;
default:
}
}
private void checkSeller(boolean b) {
double totalPrie = 0;
for (int i = 0; i < list.size(); i++) {
list.get(i).setCheck(b);
String price = list.get(i).getPrice();
Double price1 = Double.valueOf(price);
String count = list.get(i).getCount();
int numm = Integer.parseInt(count);
totalPrie = totalPrie+price1*numm;
}
if (b){
allprice.setText("合计:"+totalPrie);
}
}
}