添加数据库点击的时候

数据库的添加最新
接口回调的方法
 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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值