Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Android Studio_Android Room_Typeconverter - Fatal编程技术网

Android 如何使类型转换器与自定义类列表一起工作?

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

我有一个存储自定义对象通知列表的用户实体。 虽然我使用了类型转换器,但查询时仍然会出错

我的用户实体(省略了getter和setter)

AppDatabase类

@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;