I have created a login-register system by using Firebase auth. Now I want to show the registration information (name, email, phone) in my app. I want to reflect the registration information to text view but I couldn't find the way to do that.
Registration class:
public class RegisterActivity extends AppCompatActivity {
EditText mName, mEmail, mPassword, mPhone;
Button mRegisterButton;
TextView mLoginButton;
FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mName = findViewById(R.id.userName);
mEmail = findViewById(R.id.userMail);
mPassword = findViewById(R.id.userPassword);
mPhone = findViewById(R.id.userPhone);
mRegisterButton = findViewById(R.id.register_button);
mLoginButton = findViewById(R.id.goToLogin);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
mRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email))
{
mEmail.setError("Email is required");
return;
}
if(TextUtils.isEmpty(password))
{
mPassword.setError("Password is required");
return;
}
if(password.length() < 0)
{
mPassword.setError("Password Must Be >= 6 Characters");
return;
}
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(RegisterActivity.this, "User Created.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = firebaseAuth.getCurrentUser();
}else{
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(RegisterActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
}
});
}
}
Assuming that you have a TextView added in your layout, especially added to display the data, then right after the following line of code:
FirebaseUser user = firebaseAuth.getCurrentUser();
Add these lines:
String name = user.getDisplayName();
String email = user.getEmail();
TextView textView = findViewById(R.id.textViewId);
String text = name + "/" + email;
textView.setText(text);
Please also note that when you are using email and password authentication the phone number doesn't exist. So if you try to use it, you'll get null.
Kotlin code below;
fun onCreate(savedInstanceState: Bundle) {
val curreentUser = FirebaseAuth.getInstance().currentUser
val name = currentUser?.displayName
val email = currentUser?.email
val phone = currentUser?.phoneNumber
val textView = findViewById(R.id.your_text_view_id)
textView.text = "$name, $email, $phone"
}
Hi I want to implement clean architecture while using the realtime database but is seems that it is impossible to seperate the database from the my account screen class. Ideally I would want a DatabaseManager class that would handle all database operation (getting reading and posting to the db) but it seems that because the AuthStateChangeListener needs to be in the oncreate of my files it can't work. Does anyone know of any work-arounds? Here is the code I am using this on:
public class CreateAccountScreen extends AppCompatActivity {
private Button mRegister;
private EditText mEmail, mPassword, mCPassword, mName;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.create_account);
//------------------------------------------------------
//I would want to put this in a seperate class
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//------------------------------------------------------
mAuth = FirebaseAuth.getInstance();
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user !=null){
Intent intent = new Intent(CreateAccountScreen.this, HomeScreen.class);
startActivity(intent);
finish();
return;
}
}
};
//-----------------------------------------------------
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//I would want this in a seperate class
//------------------------------------------------------
mRegister = (Button) findViewById(R.id.goSignIn2);
mEmail = (EditText) findViewById(R.id.username3);
mPassword = (EditText) findViewById(R.id.password2);
mCPassword = (EditText) findViewById(R.id.password3);
mName = (EditText) findViewById(R.id.username2);
mRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String email = mEmail.getText().toString();
final String password = mPassword.getText().toString();
final String cPassword = mCPassword.getText().toString();
final String name = mName.getText().toString();
if (name.equals("") || password.equals("") || email.equals("") || !password.equals(cPassword)) {
Toast.makeText(CreateAccountScreen.this, "sign up error", Toast.LENGTH_SHORT).show();
return;
}
//------------------------------------------------------
//I would want to also put this in a seperate class
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//------------------------------------------------------
//DatabaseManager.createUser(email, password, name, CreateAccountScreen.this);
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(CreateAccountScreen.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()) {
Toast.makeText(CreateAccountScreen.this, "sign up error", Toast.LENGTH_SHORT).show();
}else{
String userId = mAuth.getCurrentUser().getUid();
DatabaseReference currentUserDb = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
DatabaseReference currentUserDb2 = FirebaseDatabase.getInstance().getReference().child("Users").child(userId).child("connections").child("no").child(userId);
Map userInfo = new HashMap<>();
userInfo.put("name", name);
userInfo.put("profileImageUrl", "default");
currentUserDb2.setValue(true);
currentUserDb.updateChildren(userInfo);
}
}
});
//-----------------------------------------------------
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//I would want this in a seperate class
//------------------------------------------------------
}
});
}
}
I want the data of textview in the second activity to pass in the third but i dont want to display it in the third activity. I want to directly send the data from the second activity to Pass in my sql db after clicking the button in the final activity. Is is achievable?
This is my first activity.
public class MainActivity extends AppCompatActivity {
private EditText email, password;
private Button btn_login;
private ProgressBar loading;
private static String URL_LOGIN ="http://192.168.1.83/Attendance/login.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading= findViewById(R.id.loading);
email = findViewById(R.id.editTextUserEmail);
password = findViewById(R.id.editTextPassword);
btn_login = findViewById(R.id.buttonRegister);
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mEmail = email.getText().toString().trim();
String mPassword = password.getText().toString().trim();
if (!mEmail.isEmpty() || !mPassword.isEmpty()) {
Login(mEmail, mPassword);
}else{
email.setError("Please Enter Email");
password.setError("Please Enter Password");
}
}
});
}
private void Login(final String email, final String password) {
loading.setVisibility(View.VISIBLE);
btn_login.setVisibility(View.GONE);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("JSON", jsonObject.toString());
String success = jsonObject.getString("success");
JSONArray jsonArray =
jsonObject.getJSONArray("login");
if (success.equals("1"))
{
for (int i = 0; i<jsonArray.length(); i++){
JSONObject object =
jsonArray.getJSONObject(i);
String name =
object.getString("name").trim();
String email =
object.getString("email").trim();
// Toast.makeText(MainActivity.this,
"Success Login \nYour name: "+name+"\nYour Email: "+email,
// Toast.LENGTH_LONG).show();
Intent intent = new
Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("name",name);
intent.putExtra("email", email);
startActivity(intent);
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
loading.setVisibility(View.GONE);
btn_login.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "Error"
+e.toString(),
Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error"
+error.toString(),
Toast.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
This is my second activity
public class HomeActivity extends AppCompatActivity {
private TextView name,email;
private TextView welcome;
private Button Send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
name = findViewById(R.id.name);
email = findViewById(R.id.email);
Send = findViewById(R.id.btn_send);
welcome = findViewById(R.id.welcome);
final Intent intent = getIntent();
String extraName = intent.getStringExtra("name");
String extraEmail = intent.getStringExtra("email");
name.setText(extraName);
email.setText(extraEmail);
Send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(HomeActivity.this,StatusActivity.class);
intent1.putExtra("wel", welcome.getText().toString());
intent1.putExtra("name1", name.getText().toString());
intent1.putExtra("email1", email.getText().toString());
startActivity(intent1);
}
});
}
And this is my final activity but Here i dont want to show the text view i just want to post it from here.
public class StatusActivity extends AppCompatActivity {
private TextView welcome, name, email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
welcome = findViewById(R.id.abcd);
name = findViewById(R.id.name1);
email = findViewById(R.id.email1);
final Intent intent = getIntent();
String extraName1 = intent.getStringExtra("name1");
String extraEmail1 = intent.getStringExtra("email1");
String extraWelcome = intent.getStringExtra("wel");
name.setText(extraName1);
email.setText(extraEmail1);
welcome.setText(extraWelcome);
}
Your question is pretty clear until you put MySql to the topic. Why you want to pass the data from one activity to another through the database. Why involve database operations which take time and resources.
You may pass data from one activity to another through Intents (primitive values, Strings and Parcelables)!
All you have to do is:
Get the data (int your case from TextView).
Create the Intent to navigate to the next Activity.
Put the data to the Intent.
Start the Activity.
Retrieve the data from the Intent.
For example if will navigate to the next Activity via button click then in that button's onClick():
// 1.
String data = myTextView.getText();
// 2.
Intent intent = new Intent(yourContext,YourNextActivity.class);
// 3.
intent.putStringExtra("MY DATA KEY",data);
// 4.
yourContext.startActivity(intent);
// 5. Now in YourNextActivity's onCreate();
String retrievedData = YourNextActivity.this.getIntent().getStringExtra("MY DATA KEY");
// Done!
I am using cloud Firestore as database, when I am clicking on login button, it is not updating the value at first but on second click it updates.
When I click on Login, at first it takes empty values, but on second click it takes the value entered. Same happens when I change text and press login again, it takes previous values and on second click it takes updated values.
public class LoginActivity extends AppCompatActivity {
private TextView appName;
private TextInputEditText registrationNumber;
private TextInputEditText password;
private User userSent;
private FirebaseFirestore db;
private CollectionReference collectionReference;
String result="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = FirebaseFirestore.getInstance();
db.enableNetwork();
collectionReference = db.collection("/user");
registrationNumber = findViewById(R.id.regno);
password = findViewById(R.id.password);
userSent = new User();
appName = findViewById(R.id.appname);
AssetManager assetManager = getApplicationContext().getAssets();
Typeface typeface = Typeface.createFromAsset(assetManager,"fonts/Ubuntu-Regular.ttf");
appName.setTypeface(typeface);
}
public void onLogin(View view){
userSent.setRegistrationNumber(registrationNumber.getText().toString());
userSent.setPassword(password.getText().toString());
if(!userSent.getRegistrationNumber().isEmpty() && !userSent.getPassword().isEmpty()) {
authenticateUser(userSent);
}
else{
Toast.makeText(this, "Registration Number or Password cannot be empty", Toast.LENGTH_LONG).show();
}
//Login Result
Log.d("Login Result: " , result);
if(result.contains("success")) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
else if(result.length()>0){
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
public void authenticateUser(final User sUser){
final String registration_number = sUser.getRegistrationNumber();
final String password = sUser.getPassword();
collectionReference.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(QueryDocumentSnapshot queryDocumentSnapshot: queryDocumentSnapshots){
Map<String, Object> map;
map = queryDocumentSnapshot.getData();
String reg_no = (String) map.get("registration_number");
String pass = (String)map.get("password");
if(registration_number.equals(reg_no) && password.equals(pass)){
result = "success";
return;
}
else if(registration_number.equals(reg_no)){
result = "Incorrect Password";
return;
}
else{
result = "User does not exist";
}
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
result = "Error Connecting to Server";
}
});
}
}
so I am a fresh face to android development, and I encountered a problem which I have been going loops for.
I have three activities here, they are for registration, I don't want to implement scrollview because it isn't that good looking so I made it to three form activities,
The first activity gathers all info from the XML's and passes the values as via intent to the second activity,
then the second activity gathers all info from it's own XML, and then passes it together with the Extra of the first activity to the third activity, and the third activity together with the first and seconds Extras PLUS it's own values from it's own XML's, finally, saves the values to firebase.
Yes the values get's saved, but my problem is the third activity doesn't go to the Welcome class which is supposedly written on its intent. I tried rebuilding and cleaning but it did not work. I couldn't find the bug to my work, can anyone help me out? any help is appreciated. Thanks
Here are my three java classes, and I will also put in the welcome class incase you might want to ask for it:
Profile.java
public class Profile extends Activity {
//DECLARE FIELDS
EditText name,username, phone, age, birth;
Button saveBtn;
//FIREBASE REF
DatabaseReference userRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//FIREBASE DATABASE REF
userRef = FirebaseDatabase.getInstance().getReference("Users");
//ASSIGN ID's
saveBtn = (Button) findViewById(R.id.profileBtn);
name = (EditText) findViewById(R.id.profileName);
username = (EditText) findViewById(R.id.profileUsername);
age = (EditText) findViewById(R.id.profileAge);
phone = (EditText) findViewById(R.id.profilePhone);
birth = (EditText) findViewById(R.id.profileBirth);
// SAVE BUTTON LOGIC
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addUser();
}
});
}
private void addUser(){
String userNameString = name.getText().toString();
String userPhoneString = phone.getText().toString();
String userAgeString = age.getText().toString();
String userBirthString = birth.getText().toString();
String userUserNameString = username.getText().toString();
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
String userPass = getIntent().getStringExtra("USER_PASS");
String userID = getIntent().getStringExtra("USER_ID");
if (!TextUtils.isEmpty(userNameString) && !TextUtils.isEmpty(userPhoneString) && !TextUtils.isEmpty(userAgeString) && !TextUtils.isEmpty(userBirthString) && !TextUtils.isEmpty(userUserNameString)) {
Toast.makeText(Profile.this, "Next!", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Profile.this, Profile2.class);
myIntent.putExtra("USER_KEY", userKey);
myIntent.putExtra("USER_NAME", userNameString);
myIntent.putExtra("USER_PHONE", userPhoneString);
myIntent.putExtra("USER_AGE", userAgeString);
myIntent.putExtra("USER_BIRTH", userBirthString);
myIntent.putExtra("USER_USERNAME", userUserNameString);
myIntent.putExtra("USER_EMAIL", userEmail);
myIntent.putExtra("USER_PASS", userPass);
myIntent.putExtra("USER_ID", userID);
startActivity(myIntent);
} else {
Toast.makeText(Profile.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile.this, Profile.class));
}
}
}
Profile2.java
public class Profile2 extends Activity {
//DECLARE FIELDS
EditText Address, FSN, bType, Height, Weight;
Button saveBtn;
//FIREBASE REF
DatabaseReference userRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
//FIREBASE DATABASE REF
userRef = FirebaseDatabase.getInstance().getReference("Users");
//ASSIGN ID's
saveBtn = (Button) findViewById(R.id.profileBtn);
Address = (EditText) findViewById(R.id.Address);
FSN = (EditText) findViewById(R.id.FSN);
bType = (EditText) findViewById(R.id.bType);
Height = (EditText) findViewById(R.id.Height);
Weight = (EditText) findViewById(R.id.Weight);
//MOVE TO LOGIN
// SAVE BUTTON LOGIC
saveBtn.setOnClickListener(new View.OnClickListener() {
// #RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View view) {
addUser();
}
});
}
private void addUser(){
String AddressString = Address.getText().toString();
String FSNString = FSN.getText().toString();
String WeightString = Weight.getText().toString();
String bTypeString = bType.getText().toString();
String HeightString = Height.getText().toString();
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userNameString = getIntent().getStringExtra("USER_NAME");
String userPhoneString = getIntent().getStringExtra("USER_PHONE");
String userAgeString = getIntent().getStringExtra("USER_AGE");
String userBirthString = getIntent().getStringExtra("USER_BIRTH");
String userUserNameString = getIntent().getStringExtra("USER_USERNAME");
String userPass = getIntent().getStringExtra("USER_PASS");
String userID = getIntent().getStringExtra("USER_ID");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
if (!TextUtils.isEmpty(AddressString) && !TextUtils.isEmpty(FSNString) && !TextUtils.isEmpty(WeightString) && !TextUtils.isEmpty(bTypeString) && !TextUtils.isEmpty(HeightString)) {
Toast.makeText(Profile2.this, "Go mamshie!", Toast.LENGTH_LONG).show();
Intent myIntent2 = new Intent(Profile2.this, Profile3.class);
myIntent2.putExtra("USER_KEY", userKey);
myIntent2.putExtra("USER_NAME", userNameString);
myIntent2.putExtra("USER_PHONE", userPhoneString);
myIntent2.putExtra("USER_AGE", userAgeString);
myIntent2.putExtra("USER_BIRTH", userBirthString);
myIntent2.putExtra("USER_USERNAME", userUserNameString);
myIntent2.putExtra("USER_ADDRESS", AddressString);
myIntent2.putExtra("USER_FSN", FSNString);
myIntent2.putExtra("USER_BTYPE", bTypeString);
myIntent2.putExtra("USER_HEIGHT", HeightString);
myIntent2.putExtra("USER_WEIGHT", WeightString);
myIntent2.putExtra("USER_PASS", userPass);
myIntent2.putExtra("USER_ID", userID);
myIntent2.putExtra("USER_EMAIL", userEmail);
startActivity(myIntent2);
} else {
Toast.makeText(Profile2.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile2.this, Profile2.class));
}
}
}
Profile3.java
public class Profile3 extends Activity {
//DECLARE FIELDS
EditText NPP, PCS, MC, SB, PPH;
Button saveBtn;
//FIREBASE REF
DatabaseReference mDataRef, userRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile3);
//FIREBASE DATABASE REF
userRef = FirebaseDatabase.getInstance().getReference("Users");
//ASSIGN ID's
saveBtn = (Button) findViewById(R.id.profileBtn);
NPP = (EditText) findViewById(R.id.previous);
PCS = (EditText) findViewById(R.id.previouscs);
MC = (EditText) findViewById(R.id.miscar);
SB = (EditText) findViewById(R.id.stillbirth);
PPH = (EditText) findViewById(R.id.post);
//MOVE TO LOGIN
// SAVE BUTTON LOGIC
saveBtn.setOnClickListener(new View.OnClickListener() {
// #RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View view) {
addUser();
}
});
}
private void addUser(){
String NoPrevPregnancies = NPP.getText().toString();
String PreviousCS = PCS.getText().toString();
String Miscarriages = MC.getText().toString();
String Stillbirth = SB.getText().toString();
String PostPartumHem = PPH.getText().toString();
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
String userPass = getIntent().getStringExtra("USER_PASSWORD");
String userID = getIntent().getStringExtra("USER_ID");
String userNameString = getIntent().getStringExtra("USER_NAME");
String userPhoneString = getIntent().getStringExtra("USER_PHONE");
String userAgeString = getIntent().getStringExtra("USER_AGE");
String userBirthString = getIntent().getStringExtra("USER_BIRTH");
String userUserNameString = getIntent().getStringExtra("USER_USERNAME");
String AddressString = getIntent().getStringExtra("USER_ADDRESS");
String FSNString = getIntent().getStringExtra("USER_FSN");
String bTypeString = getIntent().getStringExtra("USER_BTYPE");
String HeightString = getIntent().getStringExtra("USER_HEIGHT");
String WeightString = getIntent().getStringExtra("USER_WEIGHT");
mDataRef = userRef.child(userNameString);
if (!TextUtils.isEmpty(PreviousCS) && !TextUtils.isEmpty(NoPrevPregnancies) && !TextUtils.isEmpty(PostPartumHem) && !TextUtils.isEmpty(Miscarriages) && !TextUtils.isEmpty(Stillbirth)) {
User3 user = new User3(PreviousCS, NoPrevPregnancies, PostPartumHem, Miscarriages, Stillbirth);
userRef.child(userNameString).setValue(user);
mDataRef.child("USER_KEY").setValue(userKey);
mDataRef.child("USER_PASS").setValue(userPass);//
mDataRef.child("USER_ID").setValue(userID);//
mDataRef.child("USER_NAME").setValue(userNameString);//
mDataRef.child("USER_PHONE").setValue(userPhoneString);
mDataRef.child("USER_AGE").setValue(userAgeString);
mDataRef.child("USER_BIRTH").setValue(userBirthString);
mDataRef.child("USER_USERNAME").setValue(userUserNameString);//
mDataRef.child("USER_ADDRESS").setValue(AddressString);
mDataRef.child("USER_FSN").setValue(FSNString);
mDataRef.child("USER_BTYPE").setValue(bTypeString);
mDataRef.child("USER_HEIGHT").setValue(HeightString);
mDataRef.child("USER_WEIGHT").setValue(WeightString);
mDataRef.child("USER_EMAIL").setValue(userEmail);//
Toast.makeText(Profile3.this, "User Details Completed and Saved!", Toast.LENGTH_LONG).show();
Intent myIntent1 = new Intent(Profile3.this, Welcome.class);
myIntent1.putExtra("USER_KEY", userKey);
myIntent1.putExtra("USER_EMAIL", userEmail);
myIntent1.putExtra("USER_PHONE", userPhoneString);
myIntent1.putExtra("USER_BIRTH", userBirthString);
myIntent1.putExtra("USER_USERNAME", userUserNameString);
myIntent1.putExtra("USER_NAME", userNameString);
myIntent1.putExtra("USER_AGE", userAgeString);
myIntent1.putExtra("USER_ID", userID);
startActivity(myIntent1);
} else {
Toast.makeText(Profile3.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile3.this, Profile3.class));
}
}
}
My welcome activity which gets some data and shows it
public class Welcome extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
//ADD FIREBASE STUFF
//DECLARE FIELDS
Button outBtn;
TextView welcome;
private DatabaseReference myRef, mDataRef, userRef;
private FirebaseDatabase mFirebaseDatabase;
private String userIDPassed;
private String userID;
private String userKey;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private ListView mListView;
//FIREBASE AUTH FIELDS
private FirebaseAuth nAuth;
private FirebaseAuth.AuthStateListener nAuthlistener;
//GET USER KEY FROM INTENT
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//DRAWER LAYOUT
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
//ASSIGN IDS
outBtn = (Button) findViewById(R.id.logoutBtn);
welcome = (TextView) findViewById(R.id.WelcomeName);
mListView = (ListView) findViewById(R.id.listview);
//ASSIGN INSTANCE
myRef = FirebaseDatabase.getInstance().getReference().child("Users");
nAuth = FirebaseAuth.getInstance();
userRef = FirebaseDatabase.getInstance().getReference("Users");
FirebaseUser User = nAuth.getCurrentUser();
userID = User.getUid();
//navigation Drawer
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView mNavigationView = (NavigationView) findViewById(R.id.nav_menu);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override public boolean onNavigationItemSelected(MenuItem menuItem)
{ switch (menuItem.getItemId())
{
case(R.id.nav_account): Intent accountActivity = new Intent(getApplicationContext(), Welcome.class);
startActivity(accountActivity);
break;
case(R.id.nav_exercises): Intent accountActivity1 = new Intent(getApplicationContext(), Video.class);
startActivity(accountActivity1);
break;
case(R.id.nav_tips): Intent accountActivity2 = new Intent(getApplicationContext(), Image.class);
startActivity(accountActivity2);
break;
case(R.id.nav_scheduler): Intent accountActivity3 = new Intent(getApplicationContext(), CalendarActivity.class);
startActivity(accountActivity3);
break;
case(R.id.nav_logout): Intent accountActivity4 = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(accountActivity4);
finish();
break;
case(R.id.nav_settings): Intent accountActivity5 = new Intent(getApplicationContext(), Profile.class);
startActivity(accountActivity5);
break;
case(R.id.nav_info): Intent accountActivity6 = new Intent(getApplicationContext(), Info.class);
startActivity(accountActivity6);
break;
}
return true;
} });
//Navigation Drawer
nAuthlistener = new FirebaseAuth.AuthStateListener(){
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser User = firebaseAuth.getCurrentUser();
if (User != null){
Log.d(TAG, "onAuthStateChanged:signed_in:" + User.getUid());
Toast.makeText(Welcome.this, "Successfully signed in with: " + User.getEmail(), Toast.LENGTH_LONG).show();
}else{
Log.d(TAG, "onAuthStateChanged:signed_out" + userID);
Toast.makeText(Welcome.this, "Successfully signed out.", Toast.LENGTH_LONG).show();
}
}
};
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange (DataSnapshot dataSnapshot){
showData(dataSnapshot);
}
#Override
public void onCancelled (DatabaseError databaseError){
}
});
outBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nAuth.signOut();
finish();
startActivity(new Intent(Welcome.this, MainActivity.class));
}
});
}
private void showData(DataSnapshot dataSnapshot) {
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
String userPhone = getIntent().getStringExtra("USER_PHONE");
String userBirth = getIntent().getStringExtra("USER_BIRTH");
String userUserName = getIntent().getStringExtra("USER_USERNAME");
String userName = getIntent().getStringExtra("USER_NAME");
String userAge = getIntent().getStringExtra("USER_AGE");
String userID = getIntent().getStringExtra("USER_ID");
mDataRef = userRef.child(userName);
if (!TextUtils.isEmpty(userKey) && !TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userID) && !TextUtils.isEmpty(userBirth) && !TextUtils.isEmpty(userUserName)) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Log.d(TAG, "showData: snapshot: " + ds);
Log.d(TAG, "showData: snapshot: " + ds.child("Users"));
//display all info taken
Log.d(TAG, "showData: USER_NAME: " + userName);
Log.d(TAG, "showData: USER_AGE: " + userAge);
Log.d(TAG, "showData: USER_BIRTH: " + userBirth);
Log.d(TAG, "showData: USER_PHONE: " + userPhone);
Log.d(TAG, "showData: USER_USERNAME: " + userUserName);
ArrayList<String> array = new ArrayList<>();
array.add(userName);
array.add(userAge);
array.add(userBirth);
array.add(userPhone);
array.add(userUserName);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);
}
} else {
}
}
#Override
protected void onStart() {
super.onStart();
nAuth.addAuthStateListener(nAuthlistener);
}
#Override
protected void onStop() {
super.onStop();
nAuth.removeAuthStateListener(nAuthlistener);
}
//FOR NAVIGATION DRAWER
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
//Navigation Drawer End
}
What did I do wrong? Thinking of it as flowing water, I think the values flowed well until the last activity?
in your profile1, change the intent to profiles, profile2 eg
Toast.makeText(Profile.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile.this, Profile2.class));
and do same for profile 2 and profile 3.
Try debugging by removing your else statements and maybe add them all to the last activity