android備忘錄實現(xiàn),使用ObjectBox數(shù)據(jù)庫框架進行數(shù)據(jù)存儲,增刪改查等操作。代碼使用kotlin編寫。
1、下面看看ObjectBox數(shù)據(jù)庫封裝
需要注意的是:
? ?/**
? ? ?* 你只有配置好之后, 點擊 Make Model '你的model名字', 才會創(chuàng)建 MyObjectBox對象
? ? ?* 對于MyObjectBox的包名, 目前我發(fā)現(xiàn)的是找到第一個Entity的包名
? ? ?* 如果項目使用了Kotlin, 必須添加插件apply plugin: 'kotlin-kapt'
? ? ?* 實體Entity是不能繼承的哦.繼承的字段不會被解析
? ? ?*/
package com.mmsx.note.app
import android.content.Context
import android.util.Log
import com.elvishew.xlog.XLog
import com.mmsx.note.entity.MyObjectBox
import com.mmsx.note.entity.NoteEntity
import com.mmsx.note.entity.NoteEntity_
import io.objectbox.Box
import io.objectbox.BoxStore
import io.objectbox.android.AndroidObjectBrowser
import io.objectbox.android.BuildConfig
object ObjectBox {
lateinit var boxStore: BoxStore
private set
/**
* 你只有配置好之后, 點擊 Make Model '你的model名字', 才會創(chuàng)建 MyObjectBox對象
* 對于MyObjectBox的包名, 目前我發(fā)現(xiàn)的是找到第一個Entity的包名
* 如果項目使用了Kotlin, 必須添加插件apply plugin: 'kotlin-kapt'
* 實體Entity是不能繼承的哦.繼承的字段不會被解析
*/
fun init() : BoxStore{
boxStore = MyObjectBox.builder()
.androidContext(NoteApplication.instance.applicationContext)
.build()
if (BuildConfig.DEBUG) {
val started = AndroidObjectBrowser(boxStore).start(NoteApplication.instance)
XLog.d("ObjectBrowser", "Started: $started")
}
return boxStore
}
/**
* 重啟數(shù)據(jù)庫
* 刪除本地數(shù)據(jù)庫后,需要重新創(chuàng)建
*/
fun restartBoxStore(){
boxStore = MyObjectBox.builder().androidContext(NoteApplication.instance).build()
}
/**
* 添加數(shù)據(jù)
*/
fun addData(o: NoteEntity): Long {
try {
if (boxStore != null && !boxStore.isClosed) {
boxStore.boxFor(NoteEntity::class.java).put(o)
}
} catch (e: Throwable) {
Log.d("lyy", "error:${e.printStackTrace()}")
}
return 0
}
/**
* 更新數(shù)據(jù)(如果直接插入更新的對象,有些字段沒有值會覆蓋掉)
* 1、先查詢到數(shù)據(jù)
* 2、對查詢到的數(shù)據(jù)字段進行更新
* 3、查詢不到,直接插入
*/
fun updateData(o: NoteEntity) {
try {
if (boxStore != null && !boxStore.isClosed) {
//1、先查詢到數(shù)據(jù)
val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
val list: MutableList<NoteEntity> = box.query().equal(NoteEntity_.id, o.id).build().find()
var localBean = list.getOrNull(0)
//2、對查詢到的數(shù)據(jù)字段進行更新
localBean?.let {
localBean.title = o.title
localBean.content = o.content
localBean.updatedAt = o.updatedAt
boxStore.boxFor(NoteEntity::class.java).put(localBean)
}?: kotlin.run {
//3、查詢不到,直接插入
ObjectBox.boxStore.boxFor(NoteEntity::class.java).put(o)
}
}
} catch (e: Throwable) {
Log.d("lyy", "error:${e.printStackTrace()}")
}
}
/**
* 獲取全部對象數(shù)據(jù)
*/
fun getNoteEntityAllData(): MutableList<NoteEntity>? {
try {
if (boxStore != null && !boxStore.isClosed) {
val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
return box.all
}
} catch (e: java.lang.Exception) {
}
return ArrayList()
}
/**
* 條件查詢
*/
fun getNoteEntityData(title : String): List<NoteEntity>? {
try {
if (boxStore != null && !boxStore.isClosed) {
val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
return box.query().equal(NoteEntity_.title, title).build().find()
}
} catch (e: java.lang.Exception) {
}
return ArrayList<NoteEntity>()
}
/**
* 查詢單個數(shù)據(jù)
*/
fun getNoteEntity(id: Long): NoteEntity? {
try {
if (boxStore != null && !boxStore.isClosed) {
val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
return box[id]
}
} catch (e: java.lang.Exception) {
}
return null
}
/**
* 刪除數(shù)據(jù)單個數(shù)據(jù)1
*/
fun deleteNoteEntityData(id: Long): Boolean {
try {
if (boxStore != null && !boxStore.isClosed) {
val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
return box.remove(id)
}
} catch (e: java.lang.Exception) {
}
return false
}
/**
* 刪除數(shù)據(jù)單個數(shù)據(jù)2
*/
fun <Any> deleteData(o: Any, clazz: Class<Any>?) {
try {
if (boxStore != null && !boxStore.isClosed) {
val box: Box<Any> = boxStore.boxFor<Any>(clazz)
box.remove(o)
}
} catch (e: java.lang.Exception) {
}
}
}
2、主頁面數(shù)據(jù)展示代碼
使用kotlin代碼編寫,加上其他框架就是簡潔啊
class MainActivity : ScopedAppActivity(){
private var adapter : NoteAdapter ? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
}
private fun initView() {
adapter = NoteAdapter()
main_note_recycler_view.layoutManager = LinearLayoutManager(this)
main_note_recycler_view.adapter = adapter
adapter?.setOnItemChildClickListener { adapter, view, position ->
var intent = Intent(this, ShowNoteActivity::class.java).apply {
putExtra("entity", adapter?.data?.get(position) as NoteEntity)
}
startActivity(intent)
}
main_add_view.setOnClickListener {
startActivity(Intent(this, NoteActivity::class.java))
}
main_menu_view.setOnClickListener {
startActivity(Intent(this, SettingActivity::class.java))
}
}
override fun onResume() {
super.onResume()
val data = getNoteEntityAllData()
adapter?.setNewData(data)
if (data?.size == 0) {
main_empty_view.visibility = View.VISIBLE
}else{
main_empty_view.visibility = View.GONE
}
}
class NoteAdapter : BaseQuickAdapter<NoteEntity, BaseViewHolder>(R.layout.item_main_note) {
override fun convert(helper: BaseViewHolder, item: NoteEntity?) {
helper.setText(R.id.item_main_note_title_view, item?.title)
.setText(R.id.item_main_note_content_view, item?.content)
.setText(R.id.item_main_time_content_view, TimeUtils.date2String(item?.createdAt?.let {
Date(
it
)
},"yyyy/MM/dd HH:mm"))
.addOnClickListener(R.id.item_main_note)
}
}
}
2、數(shù)據(jù)添加和更新頁面
class NoteActivity : ScopedAppActivity(){
private var entity : NoteEntity ?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_note)
entity = this.intent.extras?.getSerializable("entity") as NoteEntity?
initView()
}
private fun initView() {
if (entity != null) {
note_title_view.text = "編輯"
note_title_edit_view?.setText(entity?.title)
note_content_edit_view.setText(entity?.content)
}
note_done_view.setOnClickListener {
if (entity == null) {
entity = NoteEntity()
entity?.title = note_title_edit_view.text.toString()
entity?.content = note_content_edit_view.text.toString()
entity?.createdAt = System.currentTimeMillis()
entity?.updatedAt = System.currentTimeMillis()
ObjectBox.addData(entity!!)
}else{
entity?.title = note_title_edit_view.text.toString()
entity?.content = note_content_edit_view.text.toString()
entity?.updatedAt = System.currentTimeMillis()
ObjectBox.updateData(entity!!)
}
finish()
}
note_back_view.setOnClickListener {
finish()
}
}
}
3、部分界面展示文章來源:http://www.zghlxwxcb.cn/news/detail-816361.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-816361.html
到了這里,關(guān)于Android 備忘錄,記事本程序設(shè)計的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!