Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何添加列表<;字符串>;我的绿岛发电机的属性?_Android_Greendao - Fatal编程技术网

Android 如何添加列表<;字符串>;我的绿岛发电机的属性?

Android 如何添加列表<;字符串>;我的绿岛发电机的属性?,android,greendao,Android,Greendao,我正在尝试使用greendao生成器生成模型,但我不太确定如何添加接受列表的属性 我知道如何使用addToMany添加列表,但是如果我需要在我的一个模型中存储ArrayList,该怎么办 大概是这样的: Entity tags = schema.addEntity("Tags"); tags.implementsInterface("android.os.Parcelable"); tags.addLongProperty("tagId").primaryKey().autoin

我正在尝试使用greendao生成器生成模型,但我不太确定如何添加接受
列表的属性

我知道如何使用
addToMany
添加
列表
,但是如果我需要在我的一个模型中存储
ArrayList
,该怎么办

大概是这样的:

Entity tags = schema.addEntity("Tags");
    tags.implementsInterface("android.os.Parcelable");
    tags.addLongProperty("tagId").primaryKey().autoincrement();
    tags.addLongProperty("date");
    tags.addArrayStringProperty("array"); // something like this
我正在考虑创建另一个实体来存储数组的所有值,并像这样对许多
进行操作

Entity myArray = schema.addEntity("MyArray");
    myArray.implementsInterface("android.os.Parcelable");
    myArray.addLongProperty("myArrayId").primaryKey().autoincrement();
    myArray.addLongProperty("id").notNull().getProperty();
    Property tagId = myArray.addLongProperty("tagId").getProperty();

ToMany tagToMyArray = tag.addToMany(myArray, tagId);
tagToMyArray.setName("tags");
myArray.addToOne(tag, tagId);
Type listType = new TypeToken<ArrayList<String>>(){}.getType();
List<String> arrayList = new Gson().fromJson(stringFromDb, listType)

您可以序列化该ArrayList,然后在greenDAO表中另存为字符串属性

String arrayString = new Gson().toJson(yourArrayList);
然后像这样把它取回来

Entity myArray = schema.addEntity("MyArray");
    myArray.implementsInterface("android.os.Parcelable");
    myArray.addLongProperty("myArrayId").primaryKey().autoincrement();
    myArray.addLongProperty("id").notNull().getProperty();
    Property tagId = myArray.addLongProperty("tagId").getProperty();

ToMany tagToMyArray = tag.addToMany(myArray, tagId);
tagToMyArray.setName("tags");
myArray.addToOne(tag, tagId);
Type listType = new TypeToken<ArrayList<String>>(){}.getType();
List<String> arrayList = new Gson().fromJson(stringFromDb, listType)
Type listType=new-TypeToken(){}.getType();
List arrayList=new Gson().fromJson(stringFromDb,listType)

另一种方法。您可以使用@convert annotation

@Entity
public class User {
@Id
private Long id;

@Convert(converter = RoleConverter.class, columnType = Integer.class)
private Role role;

public enum Role {
    DEFAULT(0), AUTHOR(1), ADMIN(2);

    final int id;

    Role(int id) {
        this.id = id;
    }
}

public static class RoleConverter implements PropertyConverter<Role, Integer> {
    @Override
    public Role convertToEntityProperty(Integer databaseValue) {
        if (databaseValue == null) {
            return null;
        }
        for (Role role : Role.values()) {
            if (role.id == databaseValue) {
                return role;
            }
        }
        return Role.DEFAULT;
    }

    @Override
    public Integer convertToDatabaseValue(Role entityProperty) {
        return entityProperty == null ? null : entityProperty.id;
    }
}
}
@实体
公共类用户{
@身份证
私人长id;
@转换(converter=RoleConverter.class,columnType=Integer.class)
私人角色;
公共枚举角色{
默认值(0)、作者(1)、管理员(2);
最终int id;
角色(int-id){
this.id=id;
}
}
公共静态类RoleConverter实现PropertyConverter{
@凌驾
公共角色convertToEntityProperty(整型数据库值){
if(databaseValue==null){
返回null;
}
for(角色:Role.values()){
if(role.id==数据库值){
返回角色;
}
}
返回Role.DEFAULT;
}
@凌驾
公共整数convertToDatabaseValue(角色entityProperty){
返回entityProperty==null?null:entityProperty.id;
}
}
}

谢谢你这么简单的回答