数据库的添加最新
接口回调的方法
public RxxpListenter rxxpListenter;
public void setRxxpListenter(RxxpListenter rxxpListenter) {
this.rxxpListenter = rxxpListenter;
}
public interface RxxpListenter{
public void RXXpShu(int v);
}
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取Adapter的Id
int adapterPosition = myViewHolder.getAdapterPosition();
if (rxxpListenter!=null){
rxxpListenter.RXXpShu(adapterPosition);
}
}
});
添加到数据库
rxxpAdapter.setRxxpListenter(new RxxpAdapter.RxxpListenter() {
@Override
public void RXXpShu(int v) {
Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
//自己定义的Bean
ZongBean zongBean=new ZongBean();
zongBean.setCommodityName(commodityList.get(v).getCommodityName());
zongBean.setPrice(commodityList.get(v).getPrice());
zongBean.setMasterPic(commodityList.get(v).getMasterPic());
DaoSession daoSession = UserDao.daoSession;
daoSession.insert(zongBean);
}
});
Fragment
package com.hsz.com.ac.frag;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hsz.com.ac.R;
import com.hsz.com.ac.UserDao;
import com.hsz.com.ac.adapter.FragTwoAdapter;
import com.hsz.com.ac.greenDao.DaoSession;
import com.hsz.com.ac.greenDao.ZongBeanDao;
import com.hsz.com.ac.show.bean.ZongBean;
import java.util.List;
/*Time:2019/4/21
*Author:hanshizhe
*Description:
*/public class frag2 extends Fragment {
private SimpleDraweeView frag2_img;
private TextView frag2_price;
private TextView frag2_text;
private RecyclerView frag2_view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment2,container,false);
frag2_view = view.findViewById(R.id.frag2_view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
frag2_view.setLayoutManager(new GridLayoutManager(getActivity(),2));
DaoSession daoSession = UserDao.daoSession;
ZongBeanDao zongBeanDao = daoSession.getZongBeanDao();
List<ZongBean> zongBeans = zongBeanDao.loadAll();
if (zongBeans!=null){
FragTwoAdapter fragTwoAdapter = new FragTwoAdapter(getActivity(), zongBeans);
frag2_view.setAdapter(fragTwoAdapter);
}
}
}