我正在使用与android连接的firebase数据库。这是我在数据库中保存数据的代码。数据存储成功,但这里有一些我无法解决的问题:
保存数据后,活动不会转移到下一个活动(即登录活动)。
自动ID也保存在数据库中,而我在代码中没有提供如何避免这种情况。
Registration.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);*/
}
}
数据库:
答案 0 :(得分: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);
每次最后一行都会运行时,它会使用/Registration/UserRegistration/$email
中的值覆盖user
处已有的内容。
如果您希望user
中的值合并与/Registration/UserRegistration/$email
,call updateChildren(...)
而不是setValue(...)
的现有值:
databaseReference.child("Registration").child("UserRegistration").updateChildren(email).setValue(user);