当前位置:   article > 正文

Android 备忘录,记事本程序设计_android备忘录设计与实现

android备忘录设计与实现

android备忘录实现,使用ObjectBox数据库框架进行数据存储,增删改查等操作。代码使用kotlin编写。

1、下面看看ObjectBox数据库封装

需要注意的是:

   /**
     * 你只有配置好之后, 点击 Make Model '你的model名字', 才会创建 MyObjectBox对象
     * 对于MyObjectBox的包名, 目前我发现的是找到第一个Entity的包名
     * 如果项目使用了Kotlin, 必须添加插件apply plugin: 'kotlin-kapt'
     * 实体Entity是不能继承的哦.继承的字段不会被解析
     */

  1. package com.mmsx.note.app
  2. import android.content.Context
  3. import android.util.Log
  4. import com.elvishew.xlog.XLog
  5. import com.mmsx.note.entity.MyObjectBox
  6. import com.mmsx.note.entity.NoteEntity
  7. import com.mmsx.note.entity.NoteEntity_
  8. import io.objectbox.Box
  9. import io.objectbox.BoxStore
  10. import io.objectbox.android.AndroidObjectBrowser
  11. import io.objectbox.android.BuildConfig
  12. object ObjectBox {
  13. lateinit var boxStore: BoxStore
  14. private set
  15. /**
  16. * 你只有配置好之后, 点击 Make Model '你的model名字', 才会创建 MyObjectBox对象
  17. * 对于MyObjectBox的包名, 目前我发现的是找到第一个Entity的包名
  18. * 如果项目使用了Kotlin, 必须添加插件apply plugin: 'kotlin-kapt'
  19. * 实体Entity是不能继承的哦.继承的字段不会被解析
  20. */
  21. fun init() : BoxStore{
  22. boxStore = MyObjectBox.builder()
  23. .androidContext(NoteApplication.instance.applicationContext)
  24. .build()
  25. if (BuildConfig.DEBUG) {
  26. val started = AndroidObjectBrowser(boxStore).start(NoteApplication.instance)
  27. XLog.d("ObjectBrowser", "Started: $started")
  28. }
  29. return boxStore
  30. }
  31. /**
  32. * 重启数据库
  33. * 删除本地数据库后,需要重新创建
  34. */
  35. fun restartBoxStore(){
  36. boxStore = MyObjectBox.builder().androidContext(NoteApplication.instance).build()
  37. }
  38. /**
  39. * 添加数据
  40. */
  41. fun addData(o: NoteEntity): Long {
  42. try {
  43. if (boxStore != null && !boxStore.isClosed) {
  44. boxStore.boxFor(NoteEntity::class.java).put(o)
  45. }
  46. } catch (e: Throwable) {
  47. Log.d("lyy", "error:${e.printStackTrace()}")
  48. }
  49. return 0
  50. }
  51. /**
  52. * 更新数据(如果直接插入更新的对象,有些字段没有值会覆盖掉)
  53. * 1、先查询到数据
  54. * 2、对查询到的数据字段进行更新
  55. * 3、查询不到,直接插入
  56. */
  57. fun updateData(o: NoteEntity) {
  58. try {
  59. if (boxStore != null && !boxStore.isClosed) {
  60. //1、先查询到数据
  61. val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
  62. val list: MutableList<NoteEntity> = box.query().equal(NoteEntity_.id, o.id).build().find()
  63. var localBean = list.getOrNull(0)
  64. //2、对查询到的数据字段进行更新
  65. localBean?.let {
  66. localBean.title = o.title
  67. localBean.content = o.content
  68. localBean.updatedAt = o.updatedAt
  69. boxStore.boxFor(NoteEntity::class.java).put(localBean)
  70. }?: kotlin.run {
  71. //3、查询不到,直接插入
  72. ObjectBox.boxStore.boxFor(NoteEntity::class.java).put(o)
  73. }
  74. }
  75. } catch (e: Throwable) {
  76. Log.d("lyy", "error:${e.printStackTrace()}")
  77. }
  78. }
  79. /**
  80. * 获取全部对象数据
  81. */
  82. fun getNoteEntityAllData(): MutableList<NoteEntity>? {
  83. try {
  84. if (boxStore != null && !boxStore.isClosed) {
  85. val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
  86. return box.all
  87. }
  88. } catch (e: java.lang.Exception) {
  89. }
  90. return ArrayList()
  91. }
  92. /**
  93. * 条件查询
  94. */
  95. fun getNoteEntityData(title : String): List<NoteEntity>? {
  96. try {
  97. if (boxStore != null && !boxStore.isClosed) {
  98. val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
  99. return box.query().equal(NoteEntity_.title, title).build().find()
  100. }
  101. } catch (e: java.lang.Exception) {
  102. }
  103. return ArrayList<NoteEntity>()
  104. }
  105. /**
  106. * 查询单个数据
  107. */
  108. fun getNoteEntity(id: Long): NoteEntity? {
  109. try {
  110. if (boxStore != null && !boxStore.isClosed) {
  111. val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
  112. return box[id]
  113. }
  114. } catch (e: java.lang.Exception) {
  115. }
  116. return null
  117. }
  118. /**
  119. * 删除数据单个数据1
  120. */
  121. fun deleteNoteEntityData(id: Long): Boolean {
  122. try {
  123. if (boxStore != null && !boxStore.isClosed) {
  124. val box: Box<NoteEntity> = boxStore.boxFor(NoteEntity::class.java)
  125. return box.remove(id)
  126. }
  127. } catch (e: java.lang.Exception) {
  128. }
  129. return false
  130. }
  131. /**
  132. * 删除数据单个数据2
  133. */
  134. fun <Any> deleteData(o: Any, clazz: Class<Any>?) {
  135. try {
  136. if (boxStore != null && !boxStore.isClosed) {
  137. val box: Box<Any> = boxStore.boxFor<Any>(clazz)
  138. box.remove(o)
  139. }
  140. } catch (e: java.lang.Exception) {
  141. }
  142. }
  143. }

2、主页面数据展示代码

使用kotlin代码编写,加上其他框架就是简洁啊

  1. class MainActivity : ScopedAppActivity(){
  2. private var adapter : NoteAdapter ? = null
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContentView(R.layout.activity_main)
  6. initView()
  7. }
  8. private fun initView() {
  9. adapter = NoteAdapter()
  10. main_note_recycler_view.layoutManager = LinearLayoutManager(this)
  11. main_note_recycler_view.adapter = adapter
  12. adapter?.setOnItemChildClickListener { adapter, view, position ->
  13. var intent = Intent(this, ShowNoteActivity::class.java).apply {
  14. putExtra("entity", adapter?.data?.get(position) as NoteEntity)
  15. }
  16. startActivity(intent)
  17. }
  18. main_add_view.setOnClickListener {
  19. startActivity(Intent(this, NoteActivity::class.java))
  20. }
  21. main_menu_view.setOnClickListener {
  22. startActivity(Intent(this, SettingActivity::class.java))
  23. }
  24. }
  25. override fun onResume() {
  26. super.onResume()
  27. val data = getNoteEntityAllData()
  28. adapter?.setNewData(data)
  29. if (data?.size == 0) {
  30. main_empty_view.visibility = View.VISIBLE
  31. }else{
  32. main_empty_view.visibility = View.GONE
  33. }
  34. }
  35. class NoteAdapter : BaseQuickAdapter<NoteEntity, BaseViewHolder>(R.layout.item_main_note) {
  36. override fun convert(helper: BaseViewHolder, item: NoteEntity?) {
  37. helper.setText(R.id.item_main_note_title_view, item?.title)
  38. .setText(R.id.item_main_note_content_view, item?.content)
  39. .setText(R.id.item_main_time_content_view, TimeUtils.date2String(item?.createdAt?.let {
  40. Date(
  41. it
  42. )
  43. },"yyyy/MM/dd HH:mm"))
  44. .addOnClickListener(R.id.item_main_note)
  45. }
  46. }
  47. }

2、数据添加和更新页面

  1. class NoteActivity : ScopedAppActivity(){
  2. private var entity : NoteEntity ?= null
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContentView(R.layout.activity_note)
  6. entity = this.intent.extras?.getSerializable("entity") as NoteEntity?
  7. initView()
  8. }
  9. private fun initView() {
  10. if (entity != null) {
  11. note_title_view.text = "编辑"
  12. note_title_edit_view?.setText(entity?.title)
  13. note_content_edit_view.setText(entity?.content)
  14. }
  15. note_done_view.setOnClickListener {
  16. if (entity == null) {
  17. entity = NoteEntity()
  18. entity?.title = note_title_edit_view.text.toString()
  19. entity?.content = note_content_edit_view.text.toString()
  20. entity?.createdAt = System.currentTimeMillis()
  21. entity?.updatedAt = System.currentTimeMillis()
  22. ObjectBox.addData(entity!!)
  23. }else{
  24. entity?.title = note_title_edit_view.text.toString()
  25. entity?.content = note_content_edit_view.text.toString()
  26. entity?.updatedAt = System.currentTimeMillis()
  27. ObjectBox.updateData(entity!!)
  28. }
  29. finish()
  30. }
  31. note_back_view.setOnClickListener {
  32. finish()
  33. }
  34. }
  35. }

3、部分界面展示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/696405
推荐阅读
相关标签
  

闽ICP备14008679号