[Android] Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用
16lz
2021-01-23
继续接上文:
Android 使用 Greendao 操作 db sqlite
布局文件:
activity_test_green.xml
<?xml version="1.0" encoding="utf-8"?>"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=" 清除数据" />
调用代码:
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: Listlist = 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
转载请著名出处!谢谢~~
转载于:https://www.cnblogs.com/wukong1688/p/10705638.html
更多相关文章
- 转:Android联系人数据库全解析
- FregServer进程,封装进程间通信数据
- Delphi获取Android下GPS的NMEA 0183数据
- Android中Json数据解析
- Android之使用HttpPost提交数据到服务器(Android手机客户端和后台
- Android:数据存储之SQLite
- Android文件系统的结构及目录用途、操作方法 整理
- Android之网络请求7————OkHttp源码4:网络操作