大家好!我正在创建一个应用程序,我希望每个用户都保存他的信息,并将其存储在我的Firebase实时数据库中。我看了很多教程,但没有用。我创建了一些简单的东西。一个按钮给出了存储命令我的问题是数据库中没有写入任何内容。我希望每个登录用户都分别存储其信息。 Firebase身份验证完成。我创建了一个UserDetails类,该类已经获得并设置了名字和名字。我只初始化了按钮和EditText,因为我删除了实时数据库的命令,因为它不起作用并且当我的应用崩溃我打开了UserDetails活动。
我
UserDetails(活动类)
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class UserDetails extends AppCompatActivity {
private FirebaseAuth auth;
private DatabaseReference reference;
private FirebaseDatabase database;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser user;
private EditText firstName;
private EditText secondName;
private Button saveInfo;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_user_details );
firstName=(EditText)findViewById ( R.id.firstname );
secondName=(EditText)findViewById ( R.id.secondname );
saveInfo=(Button)findViewById ( R.id.data );
auth=FirebaseAuth.getInstance ();
}
}
用户(用于获取和设置)
package com.example.myapplication;
public class User {
public String firstName;
public String secondName;
public String uid;
public String email;
public User(){
}
public User (String firstName, String secondName, String uid, String email) {
this.firstName = firstName;
this.secondName = secondName;
this.uid=uid;
this.email=email;
}
public User (String firstname, String secondname) {
}
public String getFirstName () {
return firstName;
}
public String getSecondName () {
return secondName;
}
public String getUid () {
return uid;
}
public String getEmail () {
return email;
}
public void setFirstName (String firstName) {
this.firstName = firstName;
}
public void setSecondName (String secondName) {
this.secondName = secondName;
}
public void setUid (String uid) {
this.uid = uid;
}
public void setEmail (String email) {
this.email = email;
}
}
实时数据库规则
{
"rules": {
".read": "auth != null",
"$user_id":{
".read": "auth.uid === $user_id",
".write": "auth.uid === $user_id"
}
}
}
实时数据库中的数据
sports-buddy-eb866
users
firstname:
true
secondname:
true
我想在用户登录后将任何信息存储在userID下方,但不写入任何内容。
答案 0 :(得分:0)
请参考我的意见
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users/{userId}");
ref.setValue(hashmap)
或ref.setValue(user)
(HashMap将包含键值对,或者user将成为User类的实例)其他链接: https://firebase.google.com/docs/database/android/read-and-write
希望这对您有所帮助,编码愉快!