【问题标题】:How to get to next activity after saving data in firebase database在firebase数据库中保存数据后如何进入下一个活动
【发布时间】:2018-02-03 15:14:43
【问题描述】:

我正在研究与 android 连接的 firebase 数据库。这是我在数据库中保存数据的代码。数据已成功存储,但这里有一些我无法解决的问题:

  1. 保存数据后,活动不会移动到下一个活动(即登录活动)。

  2. 一个自动 id 也保存在数据库中,而我没有在我的代码中提供如何避免这种情况。

注册.java

package com.example.scs.scs;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import model.user_reg;

public class Registration extends AppCompatActivity {

    TextView fname, lname, remail, r_pass, contctnum, r_hint, r_country; //declaring variables for editview
    Button btnreg;
    String f_name,l_name,email,password,contact,hint,country;


    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;


    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);





        firebaseDatabase=FirebaseDatabase.getInstance();
        databaseReference=firebaseDatabase.getReference();

        fname = (TextView) findViewById(R.id.edtfrstname);
        lname = (TextView) findViewById(R.id.edtlastname);
        remail = (TextView) findViewById(R.id.edtreg_email);
        r_pass = (TextView) findViewById(R.id.edtreg_pass);
        contctnum = (TextView) findViewById(R.id.edtcontactnum);
        r_hint = (TextView) findViewById(R.id.edtpass_hint);
        r_country = (TextView) findViewById(R.id.edtcountry);
        btnreg = (Button) findViewById(R.id.btnregister);
        Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/Gabriola.ttf");
        fname.setTypeface(myCustomFont);//font style of frst name edittext
        lname.setTypeface(myCustomFont);//font style of last name edittext
        remail.setTypeface(myCustomFont);//font style of reg email button
        r_pass.setTypeface(myCustomFont);//font style of reg pass button
        contctnum.setTypeface(myCustomFont);//font style of  contact num
        r_hint.setTypeface(myCustomFont);//font style of reg hint
        r_country.setTypeface(myCustomFont);//font style of reg country
        btnreg.setTypeface(myCustomFont);



        btnreg = (Button) findViewById(R.id.btnregister);
        btnreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("test", "onClick: clicked");
                f_name= fname.getText().toString();
                l_name=lname.getText().toString();
                email=remail.getText().toString();
               password=r_pass.getText().toString();
                hint=r_hint.getText().toString();
                country=r_country.getText().toString();
                contact=contctnum.getText().toString();

                if( f_name.isEmpty() || l_name.isEmpty() || email.isEmpty() || password.isEmpty() || hint.isEmpty() || country.isEmpty() || contact.isEmpty()){
                    fname.setError("Enter First name");
                    lname.setError("Enter Last name");
                    remail.setError("Enter Email");
                    r_pass.setError("Enter Password");
                    r_hint.setError("Enter Hint");
                    r_country.setError("Enter Country");
                    contctnum.setError("Enter Contact Number");

                    /**
                     *   You can Toast a message here that the Username is Empty
                     **/
                   // Toast.makeText(Registration.this, "First Name is required", Toast.LENGTH_LONG).show();

                }
                else {

                    user_reg user = new user_reg();
                                    user.setEmail(email);
                                    user.setFirstname(f_name);
                                    user.setLastname(l_name);
                                    user.setPassword(password);
                                    user.setHint(hint);
                                    user.setCountry(country);
                                    user.setContactno(contact);
                                    databaseReference.child("Registration").child("UserRegistration").child(email).push().setValue(user);
                    Toast.makeText(Registration.this, "Registered succesfully", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Registration.this, Login.class);
                    startActivity(intent);


                }

                    }
                });

              /*  MySQLiteHelper db = new MySQLiteHelper(getApplicationContext());

                /**
                 * CRUD Operations
                 * */
                // add user
             /* db.adduser(new user_reg(f_name,l_name,email,password,hint,country,contact));
                    Toast.makeText(Registration.this, "Registered succesfully", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Registration.this, Login.class);
                    startActivity(intent);*/




    }

}

数据库:

【问题讨论】:

  • 你能在进入下一个活动之前写一个日志语句,看看你能不能走到那一步吗?
  • 数据输入成功

标签: android firebase firebase-realtime-database


【解决方案1】:

对于 #2,无论何时您调用 push(),Firebase 都会生成一个具有唯一推送 ID 的新位置。在您的代码中,您可以:

databaseReference.child("Registration").child("UserRegistration").child(email).push().setValue(user);

如果您不想要推送 ID,只需删除对 push() 的调用并使用:

databaseReference.child("Registration").child("UserRegistration").child(email).setValue(user);

每次运行最后一行时,它都会用user 中的值覆盖/Registration/UserRegistration/$email 中的任何内容。

如果您希望user 中的值与/Registration/UserRegistration/$emailcall updateChildren(...) 的现有值合并,而不是setValue(...)

databaseReference.child("Registration").child("UserRegistration").updateChildren(email).setValue(user);

【讨论】:

  • 但我不希望 t 被覆盖。
  • 如果您不想覆盖,那么只需使用 push() ,这是分隔记录的最佳方法@asifamahmood 也是 +1
  • @asifamahmood 如果你告诉自己做什么想要什么,而不是说你不想要什么,会更容易提供帮助。我刚刚在我的答案中添加了另一个示例,该示例显示了如何将 user 与现有数据合并。
  • 我想在每次按下注册按钮时添加新用户,但没有自动生成 id。
  • 我很困惑。如果我的 sn-ps 都没有满足您的要求,请使用代码生成的 JSON 更新您的问题 您希望看到的 JSON。请确保两者都是文本(没有屏幕截图)。您可以通过单击Firebase Database console 中的“导出 JSON”链接来获取此信息。
猜你喜欢
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 2020-05-23
  • 2013-06-26
  • 1970-01-01
相关资源
最近更新 更多