數(shù)據(jù)庫(kù)greenDao

轉(zhuǎn)載自:http://www.itdecent.cn/p/1044c9cdcc97


package usung.com.n.greendaodemo;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.text.TextUtils;

import android.view.View;

import android.widget.EditText;

import android.widget.ListView;

import java.util.ArrayList;

import java.util.List;

import usung.com.n.base.BaseApplicationTwo;

import usung.com.n.R;

import usung.com.n.greendao.UserDao;

import usung.com.n.util.ToastUtil;

/**

* @author fenghui

*/

public class MainActivityextends AppCompatActivity {

private ListViewmListView;

? ? private ListmUserList =null;

? ? private MyAdaptermAdapter;

? ? private UsermUser;

? ? private UserDaomUserDao;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? mListView = findViewById(R.id.list_view);

? ? ? ? loadAllData();

? ? ? ? initView();

? ? }

void initView(){

final EditText edtInsert = findViewById(R.id.edt_insert);

? ? ? ? final EditText edtDelete = findViewById(R.id.edt_delete);

? ? ? ? final EditText edtUpdatet = findViewById(R.id.edt_update);

? ? ? ? final EditText edtQureyName = findViewById(R.id.edt_qurey_name);

? ? ? ? final EditText edtQureyId = findViewById(R.id.edt_qurey_id);

? ? ? ? (findViewById(R.id.insert)).setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

insert(edtInsert.getText().toString());

? ? ? ? ? ? }

});

? ? ? ? (findViewById(R.id.delete)).setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

delete(edtDelete.getText().toString());

? ? ? ? ? ? }

});

? ? ? ? (findViewById(R.id.update)).setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

update(edtUpdatet.getText().toString());

? ? ? ? ? ? }

});

? ? ? ? (findViewById(R.id.qurey)).setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

qurey(edtQureyId.getText().toString(), edtQureyName.getText().toString());

? ? ? ? ? ? }

});

? ? ? ? (findViewById(R.id.clear)).setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

clear();

? ? ? ? ? ? }

});

? ? }

/**

* 查詢數(shù)據(jù)庫(kù)中的所有數(shù)據(jù),并顯示到適配器上

*/

? ? public void loadAllData(){

mUserDao = BaseApplicationTwo.Companion.getInstance().getDaoSession().getUserDao();

? ? ? ? mUserList =mUserDao.loadAll();

? ? ? ? if (mUserList ==null){

mUserList =new ArrayList<>();

? ? ? ? }

mAdapter =new MyAdapter(this,mUserList);

? ? ? ? mListView.setAdapter(mAdapter);

? ? }

/**

* 增

*/

? ? public void insert(String userName){

if (TextUtils.isEmpty(userName)){

ToastUtil.showToast("請(qǐng)輸入名稱");

return;

? ? ? ? }

mUser =new User(null,userName);

? ? ? ? mUserDao.insert(mUser);

? ? ? ? mAdapter.getmUserList().clear();

? ? ? ? mAdapter.getmUserList().addAll(mUserDao.loadAll());

? ? ? ? mAdapter.notifyDataSetChanged();

? ? ? ? mListView.setSelection(mUserDao.loadAll().size()-1);

? ? }

/**

* 刪

? ? * @param id long

*/

? ? public void delete(String id){

if (TextUtils.isEmpty(id)){

ToastUtil.showToast("id不能為空");

return;

? ? ? ? }

User findUser =mUserDao.queryBuilder().where(UserDao.Properties.Id.eq(id)).build().unique();

? ? ? ? if (findUser !=null){

mAdapter.getmUserList().remove(findUser);

? ? ? ? ? ? mUserDao.deleteByKey(findUser.getId());

? ? ? ? ? ? ToastUtil.showToast("刪除成功");

? ? ? ? }else{

ToastUtil.showToast("用戶不存在");

? ? ? ? }

mAdapter.notifyDataSetChanged();

? ? }

/**

* 改

*/

? ? public void update(String id){

if (TextUtils.isEmpty(id)){

ToastUtil.showToast("id不能為空");

return;

? ? ? ? }

final User findUser =mUserDao.queryBuilder().where(UserDao.Properties.Id.eq(id)).build().unique();

? ? ? ? if (findUser !=null){

findUser.setName("fenghui" + findUser.getId());

? ? ? ? ? ? mUserDao.update(findUser);

? ? ? ? ? ? ToastUtil.showToast("修改成功");

? ? ? ? }else {

ToastUtil.showToast("用戶不存在");

return;

? ? ? ? }

mUserList =mUserDao.loadAll();

? ? ? ? mListView.post(new Runnable() {

@Override

? ? ? ? ? ? public void run() {

mListView.smoothScrollToPosition(findUser.getId().intValue());

? ? ? ? ? ? }

});

? ? ? ? mAdapter.notifyDataSetChanged();

? ? }

/**

* 查,要么按id查找,要么按照名稱查找

*/

? ? public void qurey(String id, String name){

mUserList =mAdapter.getmUserList();

? ? ? ? mUserList.clear();

? ? ? ? if (TextUtils.isEmpty(id) && TextUtils.isEmpty(name)){

ToastUtil.showToast("請(qǐng)輸入查詢條件");

? ? ? ? ? ? mAdapter.getmUserList().addAll(mUserDao.loadAll());

? ? ? ? ? ? mAdapter.notifyDataSetChanged();

return;

? ? ? ? }

if (TextUtils.isEmpty(id)){

mUserList.addAll(mUserDao.queryBuilder().where(UserDao.Properties.Name.eq(name)).build().list());

? ? ? ? }else{

mUserList.addAll(mUserDao.queryBuilder().where(UserDao.Properties.Id.eq(id)).build().list());

? ? ? ? }

mAdapter.notifyDataSetChanged();

? ? }

/**

* 清空數(shù)據(jù)庫(kù)

*/

? ? public void clear() {

mUserDao.deleteAll();

? ? ? ? mAdapter.getmUserList().clear();

? ? ? ? mAdapter.notifyDataSetChanged();

? ? }

/**

* 跟新ListView,不知為啥adapter.notifyDataSetChanged()沒(méi)反應(yīng)

*/

? ? public void notifyListView(){

mUserList.clear();

? ? ? ? mUserList =mUserDao.loadAll();

? ? ? ? mAdapter =new MyAdapter(MainActivity.this,mUserList);

? ? ? ? mListView.setAdapter(mAdapter);

? ? }

}


// BaseApplication

/**

* Descriptions:

* Created by fenghui on 2018/12/25.

*/

class BaseApplicationTwo : Application() {

override fun onCreate() {

super.onCreate()

instance =this

? ? ? ? setDatabase()

}

companion object {

private var instance : BaseApplicationTwo? =null

? ? ? ? fun getInstance() : BaseApplicationTwo {

return instance!!

}

}

private var db: SQLiteDatabase? =null

? ? private var mDaoMaster:DaoMaster? =null

? ? private var mHelper: DaoMaster.DevOpenHelper? =null

? ? private var mDaoSession: DaoSession? =null

? ? fun setDatabase() {

// 通過(guò) DaoMaster 的內(nèi)部類(lèi) DevOpenHelper,你可以得到一個(gè)便利的 SQLiteOpenHelper 對(duì)象。

// 可能你已經(jīng)注意到了,你并不需要去編寫(xiě)「CREATE TABLE」這樣的 SQL 語(yǔ)句,因?yàn)?greenDAO已經(jīng)幫你做了。

// 注意:默認(rèn)的 DaoMaster.DevOpenHelper 會(huì)在數(shù)據(jù)庫(kù)升級(jí)時(shí),刪除所有的表,意味著這將導(dǎo)致數(shù)據(jù)的丟失。

// 所以,在正式的項(xiàng)目中,你還應(yīng)該做一層封裝,來(lái)實(shí)現(xiàn)數(shù)據(jù)庫(kù)的安全升級(jí)。

? ? ? ? mHelper = DaoMaster.DevOpenHelper(this, "notes-db", null)

db =mHelper!!.writableDatabase

? ? ? ? // 注意:該數(shù)據(jù)庫(kù)連接屬于 DaoMaster,所以多個(gè) Session 指的是相同的數(shù)據(jù)庫(kù)連接。

? ? ? ? mDaoMaster = DaoMaster(db)

mDaoSession =mDaoMaster!!.newSession()

}

fun getDaoSession(): DaoSession? {

return mDaoSession

? ? }

fun getDb() : SQLiteDatabase {

return db!!

}

}


// build

applyplugin:'com.android.application'

applyplugin:'kotlin-android'

applyplugin:'org.greenrobot.greendao' // apply plugin

android {

compileSdkVersion27

? ? buildToolsVersion'26.0.2'

? ? defaultConfig {

applicationId "應(yīng)用id"

? ? ? ? minSdkVersion19

? ? ? ? targetSdkVersion27

? ? ? ? versionCode 1

? ? ? ? versionName "1.0"

? ? ? ? multiDexEnabled =true

? ? ? ? testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

? ? ? ? ndk{

// 設(shè)置支持的SO庫(kù)架構(gòu)

? ? ? ? ? ? abiFilters'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'

? ? ? ? }

}

buildTypes {

release {

minifyEnabled false

? ? ? ? ? ? proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'

? ? ? ? }

}

greendao{

schemaVersion1 // 指定數(shù)據(jù)庫(kù)schema版本號(hào),遷移等操作會(huì)用到;

? ? ? ? daoPackage'包名.greendao' // dao的包名,包名默認(rèn)是entity所在的包;

? ? ? ? targetGenDir'src/main/java' // 生成數(shù)據(jù)庫(kù)文件的目錄;

? ? }

}

dependencies {

implementation fileTree(dir:'libs',include: ['*.jar'])

implementation'com.android.support:appcompat-v7:27.1.1'

? ? implementation'com.android.support.constraint:constraint-layout:1.1.3'

? ? testImplementation'junit:junit:4.12'

? ? androidTestImplementation'com.android.support.test:runner:1.0.2'

? ? androidTestImplementation'com.android.support.test.espresso:espresso-core:3.0.2'

//? ? // 日志上報(bào)

//? ? implementation 'com.tencent.bugly:crashreport:2.6.6.1' //其中l(wèi)atest.release指代最新Bugly SDK版本號(hào),也可以指定明確的版本號(hào),例如2.2.0

//? ? implementation 'com.tencent.bugly:nativecrashreport:3.3.1' //其中l(wèi)atest.release指代最新Bugly NDK版本號(hào),也可以指定明確的版本號(hào),例如3.0

// 日志上報(bào)和熱更新

? ? compile"com.android.support:multidex:1.0.3" // 多dex配置

//注釋掉原有bugly的倉(cāng)庫(kù)

//compile 'com.tencent.bugly:crashreport:latest.release'//其中l(wèi)atest.release指代最新版本號(hào),也可以指定明確的版本號(hào),例如1.3.4

? ? compile'com.tencent.bugly:crashreport_upgrade:1.3.5'

? ? implementation'com.tencent.tinker:tinker-android-lib:1.9.6'

? ? implementation'com.tencent.bugly:nativecrashreport:3.3.1' //其中l(wèi)atest.release指代最新Bugly NDK版本號(hào),也可以指定明確的版本號(hào),例如3.0

? ? implementation'org.greenrobot:greendao:3.2.2'

? ? compile"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // greendao

}

repositories {

mavenCentral()

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容