如何在Firebase数据库中添加数据 [英] How to add data in firebase database

查看:244
本文介绍了如何在Firebase数据库中添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建停车场应用,我希望用户在注册之前在编辑文本中输入一些信息.编辑文本如下:

I am creating car park app and i want users to enter some information in edit texts before registering. The edit texts are as follows:

First NameLast NameEmailpasswordcar no.

当用户点击注册按钮时,我想将这些值存储在连接到我的项目的firebase数据库中.我想知道如何在firebase中创建表以及如何存储这些值.我是编程新手.

When user hits register button, i want to store these values in firebase database connected to my project.I want to know how to create tables in firebase and how these values will be stored. I am new to programming.

推荐答案

首先获取编辑文本:

  String email = textEmail.getText().toString().trim();
  String firstname = firstName.getText().toString().trim();
  //etc

首先使用createUserWithEmailAndPassword对用户进行身份验证,然后将其添加到数据库中:

first authenticate the user using createUserWithEmailAndPassword and then add to the database:

 private DatabaseReference mDatabase, newUser;
 private FirebaseUser mCurrentUser;
 mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
 auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toasty.info(getApplicationContext(), "creation of account was: " + task.isSuccessful(), Toast.LENGTH_SHORT).show();

                            if (!task.isSuccessful()) {
                                Toasty.error(getApplicationContext(), "Authentication failed: " + task.getException().getMessage(),
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                 mCurrentUser= task.getResult().getUser();<-- gets you the uid
                                newUser=mDatabase.child(mCurrentUser.getUid());


                                            newUser.child("email").setValue(email);
                                            newUser.child("firstname").setValue(name);



                                        }
                                    });

您将拥有以下数据库:

users
  userid
    email:userx@gmail.com <--example
    firstname:userx <--example

这篇关于如何在Firebase数据库中添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆