android - Android Room: Are getters and Setters needed for @ColumnInfo fields in @Entity classes?
问题描述
I have an entity class which is not builiding. A simple MutableLiveData<String>
with @TypeConverter
for storage
package com.example.rough;
import androidx.lifecycle.MutableLiveData;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverter;
@Entity(tableName = "RoughEntity")
public class RoughEntity {
@PrimaryKey(autoGenerate = true)
public long uid;
@ColumnInfo
private MutableLiveData<String> str=new MutableLiveData<>("Hello");
@TypeConverter
public static String toString(MutableLiveData<String> str)
{
return str.getValue();
}
@TypeConverter
public static MutableLiveData<String> toString(String str)
{
return toMutable(str);
}
private static <T> MutableLiveData<T> toMutable(T dat)
{
return new MutableLiveData<T>(dat);
}
}
This gives the build error
error: Cannot find getter for field.
private MutableLiveData<String> str=new MutableLiveData<>("Hello");
^
Adding a getter that returns LiveData<String>
doesn't solve the issue while a getter for the MutableLiveData is beside the point -- externally the private data should only be observable.
How to solve this?
Also, if one does add a getter for the Mutable, another compile error pops up asking for a setter! This is happening(in a more sophisticated example) for every other field as well.
解决方案
推荐阅读
- chef-infra - ChefSpec - 我可以模拟用户 ID(所有者)吗?我得到 Chef::Exceptions::UserIDNotFound
- r - 挖掘金融文章R
- python - 如何在 pyspark 代码中从 python 访问 emrfs 文件系统?
- java - 如何使用 Selenium Java 使用 getElementIndex
- routing - Akamai 是否支持任播来源?
- python - 如何正确格式化日期时间
- php - PHP SOAP-ERROR: Parsing Schema: element has defined when Connecting to D365 WSDL
- javascript - 如何在不告诉 fetch “mode: “no-cors” 的情况下避免 CORS 错误?
- azure - 无法访问运行 ASP.NET 4.7.1 网站的调试 Web 应用
- git - 如何在 BitBucket Server(又名 Stash)中获取标签的创建日期