• [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

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

  • 相关阅读:
    DB2 关联更新
    postgresql 开启审计日志
    Delphi D10.X中Tpath引发的单元引用及代码编写的思考
    自己写的函数或者过程与Delphi提供的重名了怎么办?(50分)
    技巧四 Delphi XE3 代码自动提示bug解决
    想开发经典界面吗?
    初涉Delphi下Windows消息机制——同一程序内自定义消息实例
    Delphi的DirectShow开发概述
    delphi接口(抄自万一)
    fkInternalCalc和fkCalculated有何区别?
  • 原文地址:https://www.cnblogs.com/wukong1688/p/10705638.html
Copyright © 2020-2023  润新知