Android 如何使类型转换器与自定义类列表一起工作?
我有一个存储自定义对象通知列表的用户实体。 虽然我使用了类型转换器,但查询时仍然会出错 我的用户实体(省略了getter和setter) AppDatabase类Android 如何使类型转换器与自定义类列表一起工作?,android,list,android-studio,android-room,typeconverter,Android,List,Android Studio,Android Room,Typeconverter,我有一个存储自定义对象通知列表的用户实体。 虽然我使用了类型转换器,但查询时仍然会出错 我的用户实体(省略了getter和setter) AppDatabase类 @Database(entities = {User.class}, version = 1) @TypeConverters({Converters.class}) public abstract class AppDatabase extends RoomDatabase { private static AppData
@Database(entities = {User.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract UserDao userDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
我发现了错误
error: Not sure how to convert a Cursor to this method's return type
及
不确定我错在哪里,因为类型转换器看起来不错。尝试在
@TypeConverters(converters.class)
下面添加@columnifo
和@SerializedName
注释到所有字段,如下所示
@TypeConverters(Converters.class)
@ColumnInfo(name = "notifications")
@SerializedName("notifications")
private List<Notifications> notifications;
请将
@ColumnInfo(name=“…”)
添加到列表通知中
条目更新了我的答案,你能试试吗
public class Notifications {
private String app;
private String type;
private String time;
public Notifications(String app, String type, String time) {
this.app = app;
this.type = type;
this.time = time;
}
}
@Database(entities = {User.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract UserDao userDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
error: Not sure how to convert a Cursor to this method's return type
The query returns some columns [notifications] which are not use by com.example.ark.example.database.Notifications. You can use @ColumnInfo annotation on the fields to specify the mapping. com.example.ark.example.database.Notifications has some fields [app, type, time] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: notifications. Fields in com.example.ark.example.database.Notifications: app, type, time.
@TypeConverters(Converters.class)
@ColumnInfo(name = "notifications")
@SerializedName("notifications")
private List<Notifications> notifications;
@SerializedName("app")
private String app;
@SerializedName("type")
private String type;
@SerializedName("time")
private String time;