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

耗尽温柔 提交于 2021-02-11 09:58:55

问题


I am using firebase as my backend. Users are authenticating using Email and Password. I'm storing additional data such as name and phone in the realtime database using push. How do i retrieve user data to display on the user profile since a uniqueid is generated. Here is the activity in which i'm storing the data

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();
                    }
                }



            }
        });


    }

Here is the activity in which the data is being retrieved

@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();
    }

Here is my realtime database

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

Should i use this method or should i use set method


回答1:


If data that your storing has a natural key, you should typically store that data under its natural key. Storing users is a great example of that. Since users are identified by their uid, it makes most sense to store the list of users keyed by their uid.

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


来源:https://stackoverflow.com/questions/39122159/in-firebase-by-using-email-and-password-authentication-i-want-to-additional-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!