繁体   English   中英

在Firebase中,通过使用电子邮件和密码身份验证,我想使用push来添加其他用户数据

[英]In firebase by using email and password authentication, i want to additional user data using push

我正在使用Firebase作为后端。 用户正在使用电子邮件和密码进行身份验证。 我正在使用push将其他数据(例如姓名和电话)存储在实时数据库中。 由于生成了唯一标识,我如何检索要显示在用户个人资料上的用户数据。 这是我存储数据的活动

public class UserActivity extends AppCompatActivity {
    private EditText inputName, inputPhone, inputNatId,inputDOB;
    private TextInputLayout inputLayoutName, inputLayoutPhone, inputLayoutNatId;
    private Button Complete;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        inputName = (EditText) findViewById(R.id.input_names);
        inputPhone = (EditText) findViewById(R.id.input_phone);
        inputNatId = (EditText) findViewById(R.id.input_natid);
        inputDOB = (EditText) findViewById(R.id.input_DOB);
        inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_names);
        inputLayoutPhone = (TextInputLayout) findViewById(R.id.input_layout_phone);
        inputLayoutNatId = (TextInputLayout) findViewById(R.id.input_layout_natid);
        Complete = (Button) findViewById(R.id.btncomplete);


        Firebase.setAndroidContext(this);






        Complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Firebase ref = new Firebase(Config.FIREBASE_URL);


                String names = inputName.getText().toString();
                String phone = inputPhone.getText().toString().trim();
                String natid = inputNatId.getText().toString().trim();
                String dob = inputDOB.getText().toString().trim();

                if (TextUtils.isEmpty(names))
                {
                    inputName.setError("Enter Full Names");
                }
                if (inputPhone.length() < 10 && inputPhone.length() > 10)
                {
                    inputPhone.setError("Phone should be 10 digits");
                }
                if (inputNatId.length() < 8 && inputNatId.length() > 8)
                {
                    inputNatId.setError("ID should be 8 digits");
                }
                if (TextUtils.isEmpty(phone))
                {
                    inputPhone.setError("Enter Phone Number");
                }
                if (TextUtils.isEmpty(natid))
                {
                    inputNatId.setError("Enter National ID");
                }
                if (TextUtils.isEmpty(dob))
                {
                    inputDOB.setError("Enter Date Of Birth");
                }
                else
                {




                    Firebase newref = ref.child("Person");

                    Map<String,String > users = new HashMap<String, String>();
                    users.put("names",names);
                    users.put("phone",phone);
                    users.put("national id",natid);
                    users.put("dob",dob);

                    newref.push().setValue(users);

                    Firebase postref = newref.push();
                    String postID = postref.getKey();


                    if (true){
                        Runnable runnable = new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),"Registration Successful",Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        };

                        android.os.Handler handler = new android.os.Handler();
                        handler.postDelayed(runnable, 1000);
                    }
                    else {
                        Toast.makeText(getApplicationContext(),"Registration Error",Toast.LENGTH_LONG).show();
                    }
                }



            }
        });


    }

这是在其中检索数据的活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        auth = FirebaseAuth.getInstance();
        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        databaseReference = FirebaseDatabase.getInstance().getReference();


        MainFragment fragment = new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();









        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        View navheaderview = navigationView.inflateHeaderView(R.layout.nav_header_main2);
        ProfilePic = (ImageView) navheaderview.findViewById(R.id.imguserpic);
        Name = (TextView) navheaderview.findViewById(R.id.tvusername);
        Email = (TextView) navheaderview.findViewById(R.id.tvuseremail);
        Email.setText(user.getEmail());


        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorlayout);

        Snackbar snackbar = Snackbar.make(coordinatorLayout, "Welcome " + user.getEmail(), Snackbar.LENGTH_LONG);
        snackbar.setAction("SignOut", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SignOut();
            }
        });
        snackbar.show();

        authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user == null){
                    startActivity(new Intent(Main2Activity.this, LoginActivity.class));
                    finish();
                }


            }
        };

        PermissionRequests();
    }

这是我的实时数据库

barapp-689c1

Person

-KP2YEDe3nhzlW0s4ckH

dob: 27/04/1996

names: Mark K

national id: 01234567

    phone: 0727286122

-KP3bKyKGh_PeHCEisjG

dob: 30/6/1990

names: Bruno M

national id: 12345698

phone: 0721241201

我应该使用此方法还是应该使用set方法

如果您存储的数据具有自然键,则通常应将该数据存储在其自然键下。 存储用户就是一个很好的例子。 由于用户由其uid标识,因此存储由uid键入的用户列表是最有意义的。

ref.child("users").child(user.getUid()).setValue(userDataThatYouWantToSave);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM