我正在尝试将数据写入 Firebase,这真的很神奇,并且在某种程度上让一切看起来都像魔法,但我很难理解它处理数据和排列数据的方式,我已经阅读了文档,并且我仍然无法成功获得我想要的东西(或者可能只是感到失落)。 我现在有一个 Client java 类,它有两个参数,我想编写 名字和姓氏
package com.example.android.bookkeepingapp;
import com.google.firebase.database.Exclude;
import java.util.HashMap;
import java.util.Map;
public class Client {
//client information
private static long clientId = 0;
private String firstName;
private String lastName;
private String companyName;
private String address;
private String email;
private String phoneNumber;
public Client()
{}
public Client(String firstName, String mLastName)
{
//take the last value and increase it by one
clientId = clientId + 1;
this.firstName = firstName;
this.lastName = getmLastName();
}
public Client(String companyName)
{
this.companyName = companyName;
}
public String getmAddress() {
return this.address;
}
public String getmCompanyName() {
return this.companyName;
}
public String getmEmail() {
return this.email;
}
public String getmFirstName() {
return this.firstName;
}
public String getmLastName() {
return this.lastName;
}
public static long getClientId() {
return clientId;
}
public String getmPhoneNumber() {
return this.phoneNumber;
}
public void setmAddress(String address) {
this.address = address;
}
public void setmCompanyName(String companyName) {
this.companyName = companyName;
}
public void setmEmail(String email) {
this.email = email;
}
public void setmFirstName(String firstName) {
this.firstName = firstName;
}
public void setmLastName(String lastName) {
this.lastName = lastName;
}
public void setmPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* Map will use for writing the items to the database.
* @return
*/
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("firstName", firstName);
result.put("lastName", lastName);
/*result.put("companyName" ,companyName);
result.put("address" ,address);
result.put("email" ,email);
result.put("phoneNumber" ,phoneNumber);*/
return result;
}
}
我想要实现的是在主数据库中有一个名为 client 的分支
mClientDatabaseReference = mFirebaseDatabase.getReference().child("client");
这是添加新用户时发生的情况
// create new client at /track-my-business/client/
final String key =FirebaseDatabase.getInstance().getReference().push().getKey();
// get user input and set it to result
// edit text
Client client = new Client(firstNameEditText.getText().toString(),
lastNameEditText.getText().toString());
Map<String, Object> clientValues = client.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(key, clientValues);
FirebaseDatabase.getInstance().getReference().updateChildren(childUpdates);
但这就是我想要的
我希望客户端的firstName和lastName都写入同一个键值中,当我执行代码时,只有firstName被成功写入。
最佳答案
updateChildren()
用于当您想要更新某个字段时。
要发送数据,您可以简单地执行以下操作:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("client").push();
ref.child("firstName").setValue(fname);
ref.child("LastName").setValue(lname);
然后你会得到这个:
client
pushid
firstName: fname
LastName:lname
pushid
//data of other client
关于java - 如何以正确的方式将java对象写入firebase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307964/