- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个关于如何使用 firebase 保存用户信息的问题。我扩展了用户身份验证并在 json 树上与用户创建了一个新节点,每个用户都有自己的 id,由 firebase 生成,用户信息在该 key /id 中。问题是,每次我这样做时:
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (number.equals(String.valueOf(user.getPhone()))) {
Log.d("here", "i entered");
key = snapshot.getKey();
userFriend = user;
}
}
我将用户信息映射到我的代码中的用户模型中的那个 key 中,但在我的项目的后续步骤中,我也需要为特定用户提供 key 。在这里,我只是在没有 key 的情况下映射用户信息。有没有一种方法可以在我的模型中添加一个字符串 id 并自动将键添加到该 id 字段?
用户模型
package com.esmad.pdm.friendlymanager.model;
import java.util.ArrayList;
public class User {
private String id;
private String username;
private int age = -1;
private String phone;
private int gamesPlayed = 0;
private ArrayList<User> FriendList = new ArrayList<User>();
// CONSTRUCTOR
public User() {
}
public User(String username) {
this.username = username;
}
// GETTERS & SETTERS
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public User(String id, String username, int age, String phone, int gamesPlayed, ArrayList<User> users) {
this.username = username;
this.age = age;
this.phone = phone;
this.gamesPlayed = gamesPlayed;
this.FriendList = users;
}
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public int getGamesPlayed() { return gamesPlayed; }
public void setGamesPlayed(int gamesPlayed) { this.gamesPlayed = gamesPlayed; }
public ArrayList<User> getUsers() { return FriendList; }
public void setUsers(ArrayList<User> users) { this.FriendList = users; }
}
最佳答案
当您调用snapshot.getValue(User.class)
时,无法获取自动注入(inject)的快照的 key 。
但是您可以轻松地添加一个额外的调用,将 key 添加到 User
对象。您首先需要为 id
添加一个 getter 和 setter 到您的 User
类:
@Exclude
public String getId() { return id; }
@Exclude
public void setId(String id) { this.id = id; }
您可能已经注意到我用 @Exclude
注释了这些。这告诉数据库客户端在读取/写入数据库时忽略这些属性。如果没有注释,您还会在数据库中的每个用户节点中获得一个 id
属性。
现在您可以在读取属性值时简单地设置和获取 key :
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
user.setId(snapshot.getKey());
System.out.println(user.getId());
}
}
在上面的代码 fragment 中,snapshot.getValue(User.class)
获取了一个具有所有常规属性的用户对象,然后是 user.setId(snapshot.getKey())
将 key 添加到该对象。
当写回数据库时,您还可以使用 user.getId()
来确定写到哪里:
ref.child(user.getId()).setValue(user);
关于android - 在模型 Firebase 上保存 userId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705871/
我已将 userid 声明为全局变量,并在函数内部为 userid 赋值并声明了一个新函数 userid()但总是在内部函数声明之前返回。为什么我无法更改全局变量。 var userid = 'tes
在 Go 中,我看到一些人像这样定义符号(方法、变量等):UserID、GetSomeURL() 以及 ids、urls 的大写字母,等 这是标准方式吗? 甚至我使用 go-lint 的编辑器也会警告
我希望获取用户在其工作站上处理每个批处理的总时间、已完成的预计总工作量、用户获得的报酬以及用户今年每天发生的故障次数。如果我可以将所有这些连接到一个查询中,那么我可以在 Excel 中使用它,并在数据
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我看到一个可重复的问题,用户使用 Meteor 服务器进行身份验证(“登录”),然后是依赖于 userId 的客户端订阅在之前更新(并且相关 UI 模板 react 性更新)Meteor.userId
从this comment来看作者:David Glasser 在 GitHub 问题中: this.userId is the primary API and Meteor.userId() is
我正在跟踪 Discover Meteor tutorial在提交 8-1 之后我们添加: // Check that the userId specified owns the documents
我只想能够在 document.ready 函数中访问变量 userid 。它可以在 JSP 页面中访问,但不能在 jQuery 中访问。我可以在同一个 JSP 页面上打印 ${userid} 的值,
我想从这个表中查询如下图 Table Image并更新从一行中选择不同 TestType、UserID 的值并将该值设置为 Final_Test_Score 列和 GROUP BY UserID我想要
两种模型“代理”和“代理”。每个都与我的“用户”模型相关联。我为每个模型指定了“user_id”的“foreignKey”。但是,Sequelize 会为一个模型在“user_id”之外创建一个“us
当用户访问我的网站时,他们单击“添加”或“删除”项目...“添加”或“删除”消息有时与应有的内容相反。例如,当您单击按钮时,它会改变颜色。如果值不是“Y”,则为灰色;如果为“Y”,则为红色。有时它是相
我被困住了,我现在知道如何提出我的问题,所以我用我想要的结果创建了一个假设场景。 我正在尝试创建一个查询,计算每个用户 ID 的水果数量,但记录水果为香蕉的用户 ID 除外。 +----------+
为了将此问题归结为基本问题和最少的代码,我创建了一个简单的新 meteor 应用程序,添加了 accounts-password打包并添加以下代码片段。我的数据库中有一个测试用户。 Meteor.me
我想通过用户的 userId 参数获取用户,但它不起作用。该应用程序连接到数据库(Atlas),我可以创建用户,批量检索它们,但我无法使用特定参数(在本例中为 UserId)检索它们,从而产生未找到错
我有点犹豫,我很想知道其他人在相同情况下的决定。 要从 RESTful API 获取由 :userId 标识的用户的 resourceType 列表,如果我有可寻址的资源,哪种 URI 模式最有意义作
我正在创建一个在线类(class)系统,因此我创建了下表 表名称=用户 -Columns: 1.userid 2.full name 3.email
我有一个表,其中包含 userId 及其一个具有长文本的属性。如下, 该表包含大约 200 万行,需要为给定的 userId 选择行(并且 userId 为 NULL 的所有行)。由于UserId为N
我是一名刚学数据库的学生... 我有两张 table 。用户和行程。 每当将新的行程添加到数据库时,我希望它触发“totalMiles”列中用户表的更新。我希望它采用 Trip.userID 并仅更改
我将 ID 列表存储在字段中(列表是指 val1,val2,val3,...),我想从表中选择行,其中该字段在列表中有 valX . 我以为这行得通,但显然行不通:( SELECT * FROM
这个问题在这里已经有了答案: ES6/ES2015 object destructuring and changing target variable (1 个回答) 去年关闭。 例如 state
我是一名优秀的程序员,十分优秀!