Reputation: 77
TLDR: I have been trying to retrieve user information from the Firebase Realtime Database, but the dataSnapshot
within the addChildEventListener or addValueEventListener seems to always return null. I followed the API exactly to no avail. Please send help!!
My Profile Activity:
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
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.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class ProfileActivity extends AppCompatActivity {
private static final String tag = "ProfileActivity";
private static final int PROFILE_ACTIVITY = 1;
//firebase elements
private FirebaseAuth mAuth;
private FirebaseDatabase mDatabase;
private DatabaseReference mReference;
private FirebaseUser currentUser;
//view elements
private Button logoutBtn;
private TextView walletBalance, tasksTaken, tasksPosted, userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
currentUser = mAuth.getCurrentUser();
mReference = mDatabase.getReference(mAuth.getUid());
Query query = mReference;
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d(tag, dataSnapshot.getValue().toString());
if(dataSnapshot.getValue() != null)
{
Log.d(tag, "not null");
User newUser = dataSnapshot.getValue(User.class);
Log.d(tag, newUser.getName());
Log.d(tag, Integer.toString(newUser.getWalletBalance()));
Log.d(tag, Integer.toString(newUser.getTasksTaken()));
Log.d(tag, Integer.toString(newUser.getTasksPosted()));
userName.setText(newUser.getName());
walletBalance.setText(newUser.getWalletBalance());
tasksTaken.setText(newUser.getTasksTaken());
tasksPosted.setText(newUser.getTasksPosted());
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
private void setViewElements() {
logoutBtn = findViewById(R.id.btn_logout);
walletBalance = findViewById(R.id.tv_walletBalance);
tasksTaken = findViewById(R.id.tv_taskSAmount);
tasksPosted = findViewById(R.id.tv_taskPAmount);
userName = findViewById(R.id.tv_userName);
}
private void goToLoginActivity() {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
finish();
}
My User Object:
public class User {
//user information
String name, email, password;
int walletBalance, tasksTaken, tasksPosted;
//constructor
public User(String name, String email, String password, int walletBalance, int tasksTaken, int tasksPosted) {
this.name = name;
this.email = email;
this.password = password;
this.walletBalance = walletBalance;
this.tasksTaken = tasksTaken;
this.tasksPosted = tasksPosted;
}
//empty constructor
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getWalletBalance() {
return walletBalance;
}
public void setWalletBalance(int walletBalance) {
this.walletBalance = walletBalance;
}
public int getTasksTaken() {
return tasksTaken;
}
public void setTasksTaken(int tasksTaken) {
this.tasksTaken = tasksTaken;
}
public int getTasksPosted() {
return tasksPosted;
}
public void setTasksPosted(int tasksPosted) {
this.tasksPosted = tasksPosted;
}
}
My Firebase Database:
Any help is very very appreciated!!
Upvotes: 0
Views: 802
Reputation: 126
Your reference is wrong. You should write "users" node first in your reference. Also you shouldn't use query without filtering or sorting.
mReference = mDatabase.getReference("users").child(mAuth.getUid());
Upvotes: 1
Reputation: 138824
When you are using the following reference:
mReference = mDatabase.getReference(mAuth.getUid());
Along with:
.addChildEventListener(/* ... */)
It means that you are trying to read all children that exist within the user object. The direct children of the user object are actually the values as are defined in your User
class. If you want to get the details of the logged-in user please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Log.d("TAG", user.getName());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidef.addListenerForSingleValueEvent(valueEventListener);
The result of the above code will be the name of the logged-in user printed out in the logcat.
Upvotes: 1