【发布时间】:2019-05-19 19:36:02
【问题描述】:
我的 json 响应是:
{
"categories": [
{
"categoryName": "Events",
"entityId": "QwN9goUaw6",
.....
}
],
"entityId": "T1nnwwpjOM"
}
我最初的计划是按照 Google 在他们的 GithubSampleBrowser 中的做法,除了他们使用 StringUtil 方法从 List Integer 更改为 List String,所以我决定改变策略并尝试遵循其他人在此处所说的内容将其转换为 String 并返回 ArrayList。
我认为我有正确的查询,我最终想要这样做,即
@Query("SELECT * FROM category_table WHERE entityId in (:entityId) ORDER BY sortIndex ASC")
public abstract LiveData<List<Category>> loadById(List<String> entityId);
我的模型目前看起来像:
类别搜索结果
@NonNull
@PrimaryKey
private String entityId;
@Nullable
private String query;
public List<Category> categories;
private String lastUpdated;
类别
@NonNull
@PrimaryKey(autoGenerate = true)
private int id;
private Integer sortIndex;
private String storyboardId;
@NonNull
private String entityId;
private String categoryName;
private String menuId;
我对转换器的尝试:
@TypeConverter
public static List<Category> fromStringToCategory(String value) {
if (value != null) {
Gson gson = new Gson();
Type type = new TypeToken<List<Category>>() {
}.getType();
List<Category> categoryList = gson.fromJson(value,type);
return categoryList;
}
return null;
}
@TypeConverter
public static String fromCategoryToString(List<Category> categories) {
if (categories != null) {
Gson gson = new Gson();
Type type = new TypeToken<List<Category>>() {
}.getType();
String json = gson.toJson(categories, type);
return json;
}
return null;
}
我认为可能更容易的是生成 categorySearchResult 的类别部分的 entityIds 列表并在它们之间建立@Relationship。我查看了有关用户和宠物的 android 文档,但无法将其与我的数据正确关联,因此非常感谢任何建议。
【问题讨论】:
-
不确定如何取消?
标签: java android android-room android-mvvm