微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

auto-droid AutoValue的简单扩展

程序名称:auto-droid

授权协议: Apache

操作系统: Android

开发语言: Java

auto-droid 介绍

针对Android开发,对Google发布的开源库Autovalue做了一个简单扩展,可以自动构建以下功能

  • createFromSharedPreferences(SharedPreferences sharedPreferences)

  • createFromCursor(Cursor cursor)

  • Parcelable implementations if the class implements Parcelable

下载

在你的build.gradle文件添加以下代码

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

repositories {
    maven { url "https://jitpack.io" }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.github.xizzhu.auto-droid:auto-droid-annotations:0.1'
    apt 'com.github.xizzhu.auto-droid:auto-droid-processors:0.1'
}

使用

从shared preference构建对象:

// annotate your Autovalue objects as usual
@Autovalue
public abstract class MyPreference {
    // indicate that the factory will retrieve the value from shared preferences
    @SharedPreference(key = "keyOfMyInt", defaultValue = "8964")
    abstract int myIntFromPreference();

    // adds the factory method
    public static MyPreference create(SharedPreferences sharedPreferences) {
        return Autovalue_MyPreference.createFromSharedPreferences(sharedPreferences);
    }
}

从cursor构建对象:

// annotate your Autovalue objects as usual
@Autovalue
public abstract class MyCursor {
    // indicate that the factory will retrieve the value from cursor
    @ColumnName("keyOfMyInt")
    abstract int myIntFromPreference();

    // adds the factory method
    public static MyPreference create(Cursor cursor) {
        return Autovalue_MyPreference.createFromCursor(cursor);
    }
}

auto-droid 官网

https://github.com/xizzhu/auto-droid

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐