【发布时间】:2017-11-18 04:21:22
【问题描述】:
如何更改下图中子节点的命名?
questions_stats 是 List<Integer>,我知道我将整数作为节点 ID,因为这是一个列表。我用 0 到 1000 之间的数字随机创建每个孩子。我将此 ID 设置为对象的一部分,并在列表中循环找到它。我想要的是在我创建它的那一刻将“0671”设置为对象的键。
我应该如何定义我的对象,以便使用我定义为字符串的 Id 访问每个孩子。
每个questions_stats 都是一个对象。
这是我的 UserProfile 类定义。
public class UserProfile implements Parcelable {
private List<Integer> questions_list;
private List<QuestionsStats> questions_stats;
private String country_name, share_code, user_name;
private int token_count;
private Boolean is_guest;
public UserProfile() {
}
public UserProfile(List<Integer> questions_list, List<QuestionsStats> questions_stats, String country_name, String share_code, String user_name, int token_count, Boolean is_guest) {
this.questions_list = questions_list;
this.questions_stats = questions_stats;
this.country_name = country_name;
this.share_code = share_code;
this.user_name = user_name;
this.token_count = token_count;
this.is_guest = is_guest;
}
}
我知道我可以单独使用child("0159").setValue(QuestionStats) 设置它们。
但出于我的目的,我需要整体检索 "user" 的数据,然后像列表一样在 questions_stats 中进行迭代。
我应该如何定义我的 UserProfile 类以实现我想要的?
谁能给我一个提示?
【问题讨论】:
-
@Peter Haddad questions_stats 是一个对象列表,因为列表 0 和 1 是索引。我想要的是能够将“0001”或“8760”之类的键设置为特定对象,并以 Map 而不是列表的形式访问 questions_stats。我可以使用四循环访问列表中的特定对象,但我相信使用 Map 会提高效率。不知道如何实现 Map。
-
Firebase数据库是一个 NoSQL 数据库,其结构为一对 key 和 veales,这意味着数据库中的每个节点都是一个Map。看到你的数据库截图,question_stats也是一个Map,其中question_stats是一个键,它下面的数据是一个值。0节点也是一个映射,其中0作为键,它下面的数据作为值。等等。不要对模型类中有一个列表感到困惑。在数据库中,每个节点都是一个Map。
标签: android json firebase firebase-realtime-database hashmap