0

Why is it when i toast it like this it really toasts the current users uid

Toast.makeText(this, "" + firebaseUser.getUid(),Toast.LENGTH_SHORT).show();

but when i do it like this

databasePassenger.child("Passenger").child(firebaseUser.getUid())
                 .addValueEventlistener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
     adapter.add(("Name: \t") +dataSnapshot.child("Name").getValue());

It displays Name: null

AL.
  • 33,241
  • 9
  • 119
  • 257
reginelae
  • 11
  • 4

1 Answers1

0

If you want use the uid in another activity please use this code.

In the FirstActivity:

if (firebaseUser != null) {
    String uid = firebaseUser.getUid();
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("uid", uid);
    startActivity(intent);
}

In the SecondActivity:

String uid = (String) getIntent().getExtras().get("uid");

Hope it helps.

Alex Mamo
  • 91,677
  • 13
  • 105
  • 138
  • somethings wrong with it, it says. java.lang.RuntimeException: Unable to start activity ComponentInfo{soco.itproject.com.hitch_passenger/soco.itproject.com.hitch_passenger.BookingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference – reginelae May 05 '17 at 11:46
  • Your `firebaseUser.getUid();` is different than `null`? – Alex Mamo May 05 '17 at 11:51
  • I haven't solve it yet sir .Maybe its because im not done yet implementing the sharedpreference – reginelae May 08 '17 at 04:14
  • Yes, another approach is to use `SharedPreferences`. Would you like me to send you a post regaring this way of solving the problem? – Alex Mamo May 08 '17 at 09:08
  • Yes sir, please!. Thank you! – reginelae May 08 '17 at 15:29
  • Please take a look at this [post](http://stackoverflow.com/questions/3851560/how-to-use-sharedpreferences). It will learn you how to store and retrieve strings with SharedPreferences. – Alex Mamo May 08 '17 at 15:36
  • this is my line of code `sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("User ID: ",userID); editor.commit(); if (editor != null){ System.out.println(editor); }` --------------------------------------------------------------- But it prints `System.out: android.app.SharedPreferencesImpl$EditorImpl@202c23b` – reginelae May 09 '17 at 04:02
  • I suggest you iterate on the content of the `SharedPreferences`. – Alex Mamo May 09 '17 at 08:39