在适配器中的应该写
rxxpViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = commodityList.get(i).getCommodityName();
String pic = commodityList.get(i).getMasterPic();
String price = commodityList.get(i).getPrice()+"";
EventBus.getDefault().post(new EventBean(name,pic,price));
}
});
App
public class App extends Application {
private static App Instance;
private DaoMaster.DevOpenHelper devOpenHelper;
private SQLiteDatabase writableDatabase;
private DaoMaster daoMaster;
private DaoSession daoSession;
public static App getInstance() {
return Instance;
}
public SQLiteDatabase getWritableDatabase() {
return writableDatabase;
}
public DaoSession getDaoSession() {
return daoSession;
}
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
//app名字
Instance = this;
//建表
devOpenHelper = new DaoMaster.DevOpenHelper(this, "a.db", null);
writableDatabase = devOpenHelper.getWritableDatabase();
daoMaster = new DaoMaster(writableDatabase);
daoSession = daoMaster.newSession();
//生成get 方法
}
}
第一个fragment
public class Shou_FragMent extends Fragment implements ShopView {
@BindView(R.id.recyclers)
RecyclerView mrecyclers;
Unbinder unbinder;
private ShopPresenterIml shopPresenterIml;
private ShopBean shopBean;
private ShopAdapter shopAdapter;
private ShopBean.ResultBean result;
private List<BaseBean> baseBeans = new ArrayList<>();
private SqlBeanDao dao;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_shou, container, false);
unbinder = ButterKnife.bind(this, view);
//获取dao层
dao = App.getInstance().getDaoSession().getSqlBeanDao();
//注册eventbus
EventBus.getDefault().register(this);
//获取p层
shopPresenterIml = new ShopPresenterIml(new ShopModelIml(), this);
//设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mrecyclers.setLayoutManager(linearLayoutManager);
//添加适配器
shopAdapter = new ShopAdapter(getActivity());
mrecyclers.setAdapter(shopAdapter);
shopPresenterIml.doshop(0,"http://172.17.8.100/small/commodity/v1/commodityList");
return view;
}
//解决内存泄漏
@Override
public void onDestroy() {
super.onDestroy();
//注销EventBus
EventBus.getDefault().unregister(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void success(String data) {
shopBean = new Gson().fromJson(data, ShopBean.class);
ShopBean.ResultBean.PzshBean pzsh = shopBean.getResult().getPzsh();
ShopBean.ResultBean.RxxpBean rxxp = shopBean.getResult().getRxxp();
ShopBean.ResultBean.MlssBean mlss = shopBean.getResult().getMlss();
baseBeans.add(pzsh);
baseBeans.add(rxxp);
baseBeans.add(mlss);
shopAdapter.setList(baseBeans);
}
@Override
public void fail(String error) {
}
//EventBus进行传值
@Subscribe(threadMode = ThreadMode.MAIN)
public void Eve(EventBean eventBean){
String name = eventBean.getCommodityName();
String pic = eventBean.getMasterPic();
String price = eventBean.getPrice();
int i = Integer.parseInt(price);
SqlBean sqlBean = new SqlBean(name,pic,i);
dao.insert(sqlBean);
}
}
public class Histroy_FragMent extends Fragment {
Unbinder unbinder;
private SqlBeanDao dao;
private RecyclerView mRecycler;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_histroy, container, false);
mRecycler= view.findViewById(R.id.recyclerViewsss);
//获取dao层
dao = App.getInstance().getDaoSession().getSqlBeanDao();
//list集合
//查询
List<SqlBean> sqlBeans = dao.loadAll();
//创建一个新的适配器
DaoMyAdapter daoMyAdapter = new DaoMyAdapter(getActivity(),sqlBeans);
mRecycler.setAdapter(daoMyAdapter);
//添加布局管理
GridLayoutManager gridLayoutManager= new GridLayoutManager(getActivity(),2);
mRecycler.setLayoutManager(gridLayoutManager);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
} else {
dao = App.getInstance().getDaoSession().getSqlBeanDao();
//list集合
//查询
List<SqlBean> sqlBeans = dao.loadAll();
//创建一个新的适配器
DaoMyAdapter daoMyAdapter = new DaoMyAdapter(getActivity(),sqlBeans);
mRecycler.setAdapter(daoMyAdapter);
}
}
}
greendao的适配器
public class DaoMyAdapter extends RecyclerView.Adapter<DaoMyAdapter.MyViewHodel> {
private Context context;
private List<SqlBean> list;
private MyViewHodel myViewHodel;
public DaoMyAdapter(Context context, List<SqlBean> list) {
this.context = context;
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public DaoMyAdapter.MyViewHodel onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_rxxp, null);
myViewHodel = new MyViewHodel(view);
return myViewHodel;
}
@Override
public void onBindViewHolder(@NonNull DaoMyAdapter.MyViewHodel myViewHodel, int i) {
myViewHodel.mTitle.setText(list.get(i).getCommodityName());
myViewHodel.mDesc.setText(list.get(i).getPrice() + "");
//Glide.with(context).load(commodityList.get(i).getMasterPic()).into(rxxpViewHolder.mImage);
myViewHodel.mImage.setImageURI(list.get(i).getMasterPic());
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHodel extends RecyclerView.ViewHolder {
private SimpleDraweeView mImage;
private TextView mTitle, mDesc;
public MyViewHodel(@NonNull View itemView) {
super(itemView);
mImage = (SimpleDraweeView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}