gpt4 book ai didi

android - 在 firebase 数据库中保存数据后如何进入下一个 Activity

转载 作者:行者123 更新时间:2023-11-30 00:08:20 25 4
gpt4 key购买 nike

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

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

  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);*/




}

}

数据库:

database

最佳答案

对于 #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/$email 中的现有值合并call updateChildren(...)而不是 setValue(...):

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

关于android - 在 firebase 数据库中保存数据后如何进入下一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com