[Android] Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用

继续接上文:

Android 使用 Greendao 操作 db sqlite

 

布局文件:

activity_test_green.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TestGreenActivity">

    <Button
        android:id="@+id/btn_all"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="optGreen"
        android:text="获取所有" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="optGreen"
        android:text="添加数据" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="optGreen"
        android:text="更新数据" />

    <Button
        android:id="@+id/btn_del"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="optGreen"
        android:text=" 删除数据" />

    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="optGreen"
        android:text=" 清除数据" />

</LinearLayout>

 

 

 

调用代码:

package com.jack.testmd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.jack.testmd.application.MyApplication;
import com.jack.testmd.greendao.DBManager;
import com.jack.testmd.greendao.UserInfoDao;
import com.jack.testmd.model.UserInfo;

import java.util.List;

public class TestGreenActivity extends AppCompatActivity {
    private final String TAG = DBManager.class.getSimpleName();
    private UserInfoDao userInfoDao = DBManager.get().getUserInfoDao();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_green);
    }

    protected void optGreen(View v) {

        switch (v.getId()) {
            case R.id.btn_all:
                List<UserInfo> list = userInfoDao.loadAll();
                for (int i = 0; i < list.size(); i++) {
                    Log.i(TAG, "id:" + list.get(i).getId() + ",name:" + list.get(i).getUserName() + ",age:" + list.get(i).getAge());
                }
                break;
            case R.id.btn_add:
                UserInfo userInfo = new UserInfo(1, "a001", 10);
                userInfoDao.insert(userInfo);
                break;
            case R.id.btn_update:
                UserInfo userInfo2 = new UserInfo(1, "b001", 10);
                userInfoDao.update(userInfo2);
                break;
            case R.id.btn_del:
                userInfoDao.deleteByKey((long) 1);
                break;
            case R.id.btn_clear:
                userInfoDao.deleteAll();
                break;
        }
    }
}

 

 

方法二:封装DaoUtils类,然后在MainActivity中调用DaoUtils

 Android 使用 Greendao 操作 db sqlite(2)-- 封装DaoUtils类

 

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10705622.html

转载请著名出处!谢谢~~

posted @ 2019-04-14 16:22  wukong1688  阅读(267)  评论(0编辑  收藏  举报