gpt4 book ai didi

android - 如何使类型转换器与自定义类列表一起使用?

转载 作者:行者123 更新时间:2023-12-03 21:11:19 27 4
gpt4 key购买 nike

我有一个存储自定义对象通知列表的用户实体。
尽管我使用了 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;
}


}

用户道接口(interface)
@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;
}
}

应用数据库类
@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.

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

最佳答案

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

关于android - 如何使类型转换器与自定义类列表一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com