greenDao数据库进行增删改查

//首先需要添加依赖

buildscript { 
      repositories {
             mavenCentral()
      }
      dependencies {
              classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' 
       }
 }


 apply plugin: 'org.greenrobot.greendao'

 dependencies {
         compile 'org.greenrobot:greendao:3.2.0'
 }

其他配置

在build.gradle(Module:app)中添加:

greendao {   
        schemaVersion 1//数据库版本号    
        daoPackage 'com.com.sky.downloader.greendao'//设置DaoMaster、DaoSession、Dao包名    
        targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录   
        //targetGenDirTest:设置生成单元测试目录    
       //generateTests:设置自动生成单元测试用例
}
//布局文件
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.greendao.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="请输入"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pass"
        android:hint="请输入"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit"
        android:hint="删除号码"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/but"
        android:text="添加"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/select"
        android:text="查询"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/delete"
        android:text="删除"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/update"
        android:text="修改"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/all"
        android:text="全部删除"
        />
    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rcy"
        ></android.support.v7.widget.RecyclerView>

</LinearLayout>
//首先需要把主界面的gerrnDao创建一下
        //实现创建
        SQLiteDatabase database = new DaoMaster.DevOpenHelper(this, "stui.db").getWritableDatabase();

        //连接
        DaoMaster master = new DaoMaster(database);

        //操作类
        DaoSession daoSession = master.newSession();
        userDao = daoSession.getUserDao();


        rcy.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false));
//点击事件  增删改查
@OnClick({R.id.but, R.id.select, R.id.delete, R.id.update, R.id.all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.but:

                insert();
                break;
            case R.id.select:
                cha();
                break;
            case R.id.delete:
                shachu();
                break;
            case R.id.update:
                upda();
                break;
            case R.id.all:
                all();
                break;

        }
    }
 //全部删除
    private void all() {

        userDao.deleteAll();

        cha();
    }
  //修改
    private void upda() {

//根据id进行修改的
        String id = edit.getText().toString().trim();

        long ids = Long.parseLong(id);


        List<User> users = userDao.loadAll();

        for(int i=0;i<users.size();i++){
            Long id1 = users.get(i).getId();
            Log.d("aa", "select: "+id1);

            if(ids==id1){
            //重新或许一遍值
                String s = name.getText().toString();
                String age = pass.getText().toString();
                User user=new User(ids,s,age);

                // user.setAge(age);
                // user.setName(s);

                userDao.update(user);

                Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this,"空的",Toast.LENGTH_SHORT).show();
            }

        }
        // User user = userDao.queryBuilder().where(UserDao.Properties.Name.eq(name)).build().unique();

        cha();

    }
//删除
    private void shachu() {
        //获取id值
        String id = edit.getText().toString().trim();

        userDao.queryBuilder().where(UserDao.Properties.Id.eq(id)).buildDelete().executeDeleteWithoutDetachingEntities();
        Toast.makeText(this, "删除成功~", Toast.LENGTH_SHORT).show();
        cha();
    }
 //查询并添加到适配器当中展示
    private void cha() {
        list = userDao.loadAll();

//适配器
        myAdapter = new MyAdapter(MainActivity.this, list);
        rcy.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
    }
 //添加
    private void insert() {
        pass1 = pass.getText().toString();
        name1 = name.getText().toString();


    //判断非空
        if (name1.isEmpty() && pass1.isEmpty()) {
            Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
        } else {
     //添加
            User student = new User(null, pass1, name1);
            long insert = userDao.insert(student);
            Log.d("aa", "insert: " + insert);


          //查询的方法
            cha();

这里写代码片


 //适配器
    class MyAdapter extends RecyclerView.Adapter {
        Context context;

        List<User> list;

        public MyAdapter(Context context, List<User> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View inflate = View.inflate(context, R.layout.item_layout, null);
            MyHolder myHolder = new MyHolder(inflate);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            MyHolder holder1 = (MyHolder) holder;

            holder1.name.setText(list.get(position).getName() + "---");
            holder1.pass.setText(list.get(position).getAge() + "---");
            holder1.ide.setText(list.get(position).getId() + "");


        }

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

    class MyHolder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final TextView pass;
        private final TextView ide;

        public MyHolder(View view) {
            super(view);

            name = view.findViewById(R.id.name);

            pass = view.findViewById(R.id.pass);

            ide = view.findViewById(R.id.id);
        }
    }

    }

猜你喜欢

转载自blog.csdn.net/l6666_6666/article/details/80400543
今日推荐