Code Monkey home page Code Monkey logo

easynote's Introduction

EasyNote

使用Kotlin开发的仿华为手机EMUI备忘录,数据本地存储使用ObjectBox

Kotlin-ObjectBox的使用

1、工程配置

buildscript {
    ext.kotlin_version = '1.1.3-2'
    ext.objectBoxVersion = "0.9.13"
    repositories {
        google()
        jcenter()
        maven { url "http://objectbox.net/beta-repo/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha9'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.objectbox:objectbox-gradle-plugin:$objectBoxVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "http://objectbox.net/beta-repo/" }
    }
}
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile('org.jetbrains.anko:anko:0.10.0') {
    exclude group: 'com.google.android', module: 'android'
}
compile "io.objectbox:objectbox-android:$objectBoxVersion"
compile "io.objectbox:objectbox-kotlin:$objectBoxVersion"
kapt "io.objectbox:objectbox-processor:$objectBoxVersion"

2、配置KotlinBean--->make project

@Entity
data class Note(
    @Id var id: Long = 0,
    var type: String? = null,
    var content: String? = null,
    var date: Date? = null
)

3、使用ObjectBox(基本使用)

class App : Application() {

    lateinit var boxStore: BoxStore
        private set

    override fun onCreate() {
        super.onCreate()
        instance = this
        boxStore = MyObjectBox.builder().androidContext(this).build()
    }
}

private lateinit var noteBox: Box<Note>
private lateinit var noteQuery: Query<Note>

noteBox = (application as App).boxStore.boxFor(Note::class.java)
notesQuery = noteBox.query().order(Note_.content).build()

增、改

private fun addNote() {
    val content = contentTv.text.toString()
    val note = Note(type = type, content = content, date = date)
    if (noteID != -1L) {    //不传ID则ID自增,传入ID则为改
        note.id = noteID
    }
    noteBox.put(note)
}

noteBox.remove(note)

fun queryNotes() {      //查询全部
    val notes = noteQuery.find()
    mAdapter.setData(notes)
}

fun queryNotes(type: String) {      //查询指定type
    val builder: QueryBuilder<Note> = noteBox.query()
    val notes = builder.equal(Note_.type, type).build().find()
    mAdapter.setData(notes)
}

EasyNote预览

image image image image image

相关链接

1、GreenDao老东家的ObjectBox

2、Google 2017IO大会指定Android开发语言Kotlin

3、Anko是一个使开发Android应用更简单更快捷的库Anko

更新日志

1、升级objectBoxVersion

easynote's People

Contributors

leiyun1993 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.