房间类型转换器 [英] Room TypeConverter

查看:110
本文介绍了房间类型转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定房间有什么大惊小怪的,因为我发现它很烂.几乎没有任何效果像预期的那样.对于我的问题,我想测试一下它们拥有的类型转换器(到目前为止令人失望).为了测试它,我从网上看到的一个示例制作了一个简单的类.我敢肯定这是愚蠢的,甚至与代码无关,但是无论如何我都会在这里尝试一下.到目前为止,我有:

I am not sure what is the fuss about Room as I find it very crappy. Almost nothing works as expected. To my issue I want to test the type converter thingy they have (so far disappointed). To test it I made a simple class from an example I saw online. I am sure it is something stupid not even related to the code but I will give it a try here anyway. So far I have:

@Entity
public class User {

    @PrimaryKey
    private int id;

    private String name;

    @TypeConverters(Converters.class)
    List<String> pets = new ArrayList<>();

    public User() { }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getPets() {
        return pets;
    }

    public void setPets(List<String> pets) {
        this.pets = pets;
    }
}

然后转换程序:

public class Converters {
    @TypeConverter
    public static ArrayList<String> fromString(String value) {
        Type listType = new TypeToken<ArrayList<String>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }
    @TypeConverter
    public static String fromArrayList(ArrayList<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}

一个简单的道:

@Dao
public interface UserDao {

    @Insert(onConflict = REPLACE)
    void insertUser(User user);

    @Query("SELECT * FROM User")
    List<User> getUsers();
}

最后是数据库:

@Database(entities = { User.class,}, version = 1, exportSchema = false)
@TypeConverters({Converters.class})
public abstract class RoomDb extends RoomDatabase {

    public abstract UserDao userDao();

}

我仍然得到无法弄清楚如何将该字段保存到数据库中.您可以考虑为其添加类型转换器".没有任何详细信息说明转换器为何不工作或发生了什么.为了避免麻烦,我已经尝试过像其他解决方案所建议的那样,在自己的字段上使用@Converters.

And still I get "Cannot figure out how to save this field into database. You can consider adding a type converter for it" shit. No details why the converter is not working or what's so ever. To save the trouble I already tried to use the @Converters on the field it self like the alternative solution suggests.

到目前为止,Room让我对复杂的问题感到失望,当您的模型相当复杂时,我发现Realm和ObjectBox更加一致.

Room has left me with disappointment so far for complicated problems, and I find Realm and ObjectBox to be more consistent when your model is fairly complicated.

凭您的见解随时加入斗争.

Feel free to join the struggle with your insight.

推荐答案

而不是Converters类中的ArrayList.尝试列表.您的实体正在使用List作为其声明的类.可能有问题.

Instead of ArrayList in the Converters class. Try List. Your entity is using List as its declared class. There might be a problem with that.

这是上述克诺索斯(Knossos)建议的解决方案.我最终使用ArrayList对其进行了测试并开始工作.

That was the solution as suggested from Knossos above. I ended up testing it with ArrayList and worked.

这篇关于房间类型转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆