Obtaining user ID from database and using that ID in the database reference for FirebaseRecyclerView
See original GitHub issueI understand this isn’t particularly an issue, and I can’t find anything online to help me with this.
I have a MainActivity
class that signs the user in with Firebase and gets the current user there, and assigns it to a global static clientUser variable. I have a fragment in another class (that the MainActivity uses) that contains the FirebaseRecyclerView
and uses the clientUser
object to get its firebase ID.
How would I add that user’s firebase ID into a database reference so that my FirebaseRecyclerView loads up that particular user’s groups from the database into the RecyclerView?
Basically, I get a null ptr exception at the initialization of mRef here because clientUser
has not been created yet, because Firebase didnt log in yet. Any help/advice would be appreciated!
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users").child(clientUser.get_fb_uid()).child(groups);
groupRecyclerViewAdapter = new FirebaseRecyclerAdapter<Group, GroupHolder>(Group.class, R.layout.grouptask_item, GroupHolder.class, mRef) {
@Override
protected void populateViewHolder(GroupHolder viewHolder, Group group, int position) {
viewHolder.setCardGroupName(group.getGroupName());
viewHolder.setGroupMemberCount(group.getMembers().size());
viewHolder.setCardAuthorProfileImg(group.getAuthorProfilePictureUrl());
}
};
Never mind! After hours of experimenting I realized I can just add the following in my fragment class!
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grouptask_item_list, container, false);
itemTouchHelper = new ItemTouchHelper(simpleCallbackItemTouchHelper);
Context context = view.getContext();
recyclerView = (RecyclerView) view;
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "User signed in... Collecting FireBase currentUser info");
// Currently signed in
groupRecyclerViewAdapter = new FirebaseRecyclerAdapter<Group, GroupHolder>(Group.class, R.layout.grouptask_item, GroupHolder.class, mRef.child(user.getUid()).child("groups")) {
@Override
protected void populateViewHolder(GroupHolder viewHolder, Group group, int position) {
viewHolder.setCardGroupName(group.getGroupName());
viewHolder.setGroupMemberCount(group.getMembers().size());
viewHolder.setCardAuthorProfileImg(group.getAuthorProfilePictureUrl());
}
};
recyclerView.setAdapter(groupRecyclerViewAdapter);
} else {
Log.d(TAG, "User is currently signed out");
}
}
};
itemTouchHelper.attachToRecyclerView(recyclerView);
return view;
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:8
Top GitHub Comments
@Joroze You should probably wait for the user to sign in… Firebase Auth provides a nice callback for this:
In your
onCreate
andonDestroy
:PS: You really shouldn’t be storing the
uid
in a static variable. That will be full of bugs and weirdness when Android tries to restore your activities from a saved state. UseFirebaseAuth.getInstance().getCurrentUser().getUid()
insead.@Joroze You’re welcome! 😄