在firebase中使用电子邮件和密码身份验证,我希望使用push来增加用户数据

时间:2016-08-24 11:31:24

标签: android firebase-realtime-database firebase-authentication

我使用firebase作为我的后端。用户使用电子邮件和密码进行身份验证。我使用push在实时数据库中存储名称和电话等附加数据。由于生成了uniqueid,如何检索要在用户配置文件上显示的用户数据。 这是我存储数据的活动

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方法

1 个答案:

答案 0 :(得分:2)

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

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