【问题标题】:How to make typeconverters work with list of custom class?如何使类型转换器与自定义类列表一起使用?
【发布时间】:2026-01-07 19:25:01
【问题描述】:

我有一个存储自定义对象通知列表的用户实体。 尽管我使用了 TypeConverters,但我仍然收到查询错误。

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

@Entity(tableName = "User")
public class User {
    @PrimaryKey(autoGenerate = true)
    private int userId;

    @ColumnInfo(name = "username")
    private String username;

    @ColumnInfo(name = "number")
    private String number;

    @TypeConverters(Converters.class)
    private List<Notifications> notifications;

    public User(String username, String number, List<Notifications> notifications) {
        this.username = username;
        this.number = number;
        this.notifications = notifications;
    }


}

userDao 接口

@Dao
public interface UserDao {
    @Query("SELECT * FROM User")
    List<User> getAll();

    @Query("SELECT COUNT(*) from User")
    int countUsers();

    @Query("SELECT notifications from User where userId LIKE :userId")
    List<Notifications> getNotifications(int userId);

    @Insert
    void insert(User... users);

    @Delete
    void delete(User... users);

    @Update
    void update(User... users);

} 

转换器类

public class Converters {
    @TypeConverter
    public static List<Notifications> fromString(String value) {
        Type listType = new TypeToken<List<Notifications>>() {}.getType();
        List<Notifications> notifications = new Gson().fromJson(value,listType);
        return notifications;
    }

    @TypeConverter
    public static String listToString(List<Notifications> list) {
        Gson gson = new Gson();
        return gson.toJson(list);
    }
} 

通知类(省略getter和setter)

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

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

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.

不知道我做错了什么,因为类型转换器对我来说看起来不错。

【问题讨论】:

    标签: android list android-studio android-room typeconverter


    【解决方案1】:

    尝试在@TypeConverters(Converters.class)下方添加@ColumnInfo(name = "notifications")

    【讨论】:

      【解决方案2】:

      请将@ColumnInfo(name = "........") 添加到List&lt;Notifications&gt; notifications 进入

      【讨论】:

        【解决方案3】:

        尝试在所有字段中添加@ColumnInfo@SerializedName 注释,如下所示。

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

        【讨论】:

        • 更新了我的答案,你能试试吗
        最近更新 更多