Firebase对象未写入数据库。 什么是错误。 。该活动正在运行,没有错误,我没有在firebase数据库中写入值。怎么做
这是我的java文件
package com.shaan.homely;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.shaan.homely.pojo.User;
import java.util.HashMap;
import java.util.Map;
public class Empty extends AppCompatActivity {
FirebaseDatabase firebaseDatabase;
DatabaseReference myref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty);
User user = new User("shaan","shaanthenapster@gmail.com","9718460374");
myref = FirebaseDatabase.getInstance().getReference();
String userId = myref.push().getKey();
Map<String,User> data = new HashMap<String,User>();
data.put(userId,user);
//Toast.makeText(getApplicationContext(),myref.toString(),Toast.LENGTH_LONG).show();
// creating user object
myref.child("users").child(userId).setValue(data);
myref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Toast.makeText(getApplicationContext(),user.getEmail()+user.getName(),Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}
});
myref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
User user = dataSnapshot.getValue(User.class);
Toast.makeText(getApplicationContext(),user.getEmail()+user.getName(),Toast.LENGTH_LONG).show();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}
});
}
}
这是我的用户类
package com.shaan.homely.pojo;
import com.google.firebase.database.IgnoreExtraProperties;
@IgnoreExtraProperties
public class User {
public String name;
public String email;
public String mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public User() {
}
public User(String name, String email, String mobile) {
this.name = name;
this.email = email;
this.mobile = mobile;
}
}
答案 0 :(得分:0)
您将值推送到根数据库,然后在子用户&#39;中查找它。您的初步参考必须指向孩子的用户&#39; ,您希望创建新条目:
myref = FirebaseDatabase.getInstance().getReference().child("users");
String userId = myref.push().getKey();
Map<String,User> data = new HashMap<String,User>();
data.put(userId,user);
//Toast.makeText(getApplicationContext(),myref.toString(),Toast.LENGTH_LONG).show();
// creating user object
myref.child(userId).setValue(data);
<强>更新强>
您也在推送一个hashmap,但随后又读了一个User类。我建议你将id添加到User类而不是创建hashmap:
public class User {
...
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
在您的代码中:
String userId = myref.push().getKey();
user.setUserId(userId);
// creating user object
myref.child("users").child(userId).setValue(user);