java - 如何获取要显示的数据?我的 Firebase 实时数据库仍然显示为空
问题描述
我正在尝试将我的 Realtime Firebase 数据库与我的代码同步。我目前正在使用 Firebase 身份验证,并且能够获取用户 ID,但我的实时数据库保持为空。我的 Firebase 实时数据库规则当前设置为 true。我的代码有问题吗?我正在尝试将用户 ID、名字和姓氏拉入 Firebase 数据库。
public class Add_Info_After_Registration extends AppCompatActivity {
private EditText FirstName, LastName;
private ImageButton RegisterInfoButton;
private FirebaseAuth mAuth;
private DatabaseReference UsersReference;
//Firebase Things//
String currentUserID;
//Firebase Things//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__info__after__registration);
//this is referring to storing username information in firebase//
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersReference = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
//this is referring to storing username information in firebase//
FirstName = (EditText) findViewById(R.id.add_info_first_name);
LastName = (EditText) findViewById(R.id.add_info_last_name);
RegisterInfoButton = (ImageButton) findViewById(R.id.register_submit_button);
mAuth = FirebaseAuth.getInstance();
RegisterInfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
SaveAccountSetUpInformation();
SendUserToMainActivity();
}
});
}
private void SaveAccountSetUpInformation()
{
String firstname = FirstName.getText().toString();
String lastname = LastName.getText().toString();
if(TextUtils.isEmpty(firstname))
{
Toast.makeText(this, "Please insert first name", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(lastname))
{
Toast.makeText(this, "Please insert last name", Toast.LENGTH_SHORT).show();
}
else
{
HashMap userMap = new HashMap();
userMap.put("firstname", firstname);
userMap.put("lastname", lastname);
//this is referring to storing username information in firebase//
UsersReference.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task)
{
if(task.isSuccessful())
{
SendUserToMainActivity();
Toast.makeText(Add_Info_After_Registration.this, "Your Account is Created Sucessfully", Toast.LENGTH_LONG).show();
}
else
{
String message = task.getException().getMessage();
Toast.makeText(Add_Info_After_Registration.this, "Error Occured:"+ message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void SendUserToMainActivity()
{
Intent setupIntent = new Intent(Add_Info_After_Registration.this, MainActivity.class);
setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(setupIntent);
finish();
}
}
解决方案
推荐阅读
- android - androidx.appcompat 库对非 SDK 接口的限制
- python - 这个 Python 程序的错误是什么?
- java - Android:从 BroadcastReceiver 检索到的静态变量与在 MainActivity 中检索到的相同变量不同?
- python - 在 Python 中建模制造风能曲线
- qt - CloudViewer 和 PCLVisualizer 未关闭
- opencv-python - 视频流中的多行跟踪
- spring - 在休眠和 STS 中使用逆向工程更新数据库模式后更新实体类
- django - ISR 在 django 服务器中不工作,但在下一个开发服务器中工作
- php - 嗨,我怎样才能让这个 curl 先通过代理,无论是 socks 4,5 还是 https、http?在php中
- android - 为什么我收到此错误“SDK 位置在工作室安装位置内。” 在安卓工作室