从网络请求数据添加到数据库

//布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="xal.cn.com.moni_one.Main2Activity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="主页"
        android:textSize="20sp" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mrecy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
LinearLayout>
//代码
 
  
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jiyun.com.day07_greendao.DaoMaster;
import com.jiyun.com.day07_greendao.DaoSession;
import com.jiyun.com.day07_greendao.EntityDao;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Main2Activity extends AppCompatActivity {
ArrayList<Bean.DataBean> arrayList =new ArrayList<>();

    @BindView(R.id.mrecy)
    RecyclerView mrecy;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        initData();
        adapter = new MyAdapter(this,arrayList);
        mrecy.setAdapter(adapter);
        mrecy.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        adapter.setOnClickLinnser(new MyAdapter.Linnerser() {
            @Override
            public void onClick(final int position) {
                AlertDialog.Builder builder=new AlertDialog.Builder(Main2Activity.this);  //先得到构造器
                //设置标题
                builder.setMessage("是否添加到数据库?"); //设置内容
                builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss(); //关闭dialog
                        SQLiteDatabase w = DaoUtil.getIn(Main2Activity.this).getW();
                        DaoMaster master = new DaoMaster(w);
                        DaoSession daoSession = master.newSession();
                        EntityDao entityDao = daoSession.getEntityDao();
                        Entity entity =new Entity();
                        entity.setId(null);
                        entity.setTitle(arrayList.get(position).getGoods_name());
                        entity.setCount(arrayList.get(position).getEfficacy());
                        entity.setImage(arrayList.get(position).getGoods_img());
                        entityDao.insert(entity);
                        Intent intent =new Intent(Main2Activity.this,Main3Activity.class);
                        startActivity(intent);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Toast.makeText(Main2Activity.this, "取消" + which, Toast.LENGTH_SHORT).show();
                    }
                });
                //参数都设置完成了,创建并显示出来
                builder.create().show();


            }
        });
    }



    private void initData() {
        OkhttpUtils.getInstance().getNetData("http://m.yunifang.com/yunifang/mobile/goods/getall?random=60305&encode=b0358434fda8d7478bfc325b5828b721&category_id=17", new OkhttpUtils.CallBacks() {
            @Override
            public void getString(String ss) {
                Gson gson =new Gson();
                Bean bean = gson.fromJson(ss, Bean.class);
                List<Bean.DataBean> data = bean.getData();
                arrayList.addAll(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

//适配器
 
  
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    @BindView(R.id.mimage)
    ImageView mimage;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.count)
    TextView count;
    private Context context;
List<Bean.DataBean> mlist;

    public MyAdapter(Context context, List<Bean.DataBean> mlist) {
        this.context = context;
        this.mlist = mlist;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        return new MyHolder(view);
    }

    //
    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        Glide.with(context).load(mlist.get(position).getGoods_img()).into(holder.iv);
        holder.textView.setText(mlist.get(position).getGoods_name());
        holder.textView1.setText(mlist.get(position).getEfficacy());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                linnerser.onClick(position);
            }
        });

    }
    public void setOnClickLinnser(Linnerser linnsers){
        this.linnerser=linnsers;
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private  ImageView iv;
        private  TextView textView,textView1;

        public MyHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.mimage);
            textView = itemView.findViewById(R.id.title);
            textView1=itemView.findViewById(R.id.count);

        }
    }
    Linnerser linnerser;
    public interface Linnerser{
        void onClick(int position);

    }
}
//数据库的界面
 
  
//布局文件
 
  
<ListView
    android:id="@+id/mlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
//代码
 
  
public class Main3Activity extends AppCompatActivity {

    @BindView(R.id.mlist)
    ListView mlist;
    private OneAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        ButterKnife.bind(this);

        cha();

    }

    private void cha() {
        SQLiteDatabase r = DaoUtil.getIn(this).getR();
        DaoMaster daoMaster = new DaoMaster(r);
        DaoSession session = daoMaster.newSession();
        EntityDao dao = session.getEntityDao();
        List<Entity> list = dao.queryBuilder().list();
        adapter = new OneAdapter(list, this);
        mlist.setAdapter(adapter);
}
 
  

你可能感兴趣的:(从网络请求数据添加到数据库)