question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Obtaining user ID from database and using that ID in the database reference for FirebaseRecyclerView

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
SUPERCILEXcommented, Apr 16, 2017

@Joroze You should probably wait for the user to sign in… Firebase Auth provides a nice callback for this:

@Override
public void onAuthStateChanged(@NonNull FirebaseAuth auth) {
    if (auth.getCurrentUser() == null) {
        // Not signed in
    } else {
        // Signed in, init your Firebase recyclerview here
    }
}

In your onCreate and onDestroy:

FirebaseAuth.getInstance().addAuthStateListener(this); // In onCreate

FirebaseAuth.getInstance().removeAuthStateListener(this); // In onDestroy

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. Use FirebaseAuth.getInstance().getCurrentUser().getUid() insead.

1reaction
SUPERCILEXcommented, Apr 16, 2017

@Joroze You’re welcome! 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrieving Uid's from Firebase Database - Stack Overflow
I am currently working on a chatting app. My objective is to get the particular id(marked in the picture)on clicking on that username...
Read more >
Work with Lists of Data on Android - Firebase - Google
This document covers working with lists of data in Firebase. To learn the basics of reading and writing Firebase data see Read and...
Read more >
How to populate RecyclerView with Firebase data using ...
Step 1: Open Android Studio and create a new project named “RecyclerView” with an empty activity. Step 2: Connect your Firebase project with...
Read more >
retrieve data from firebase to recyclerview - YouTube
In this video we will retrieve data from firebase to recyclerview in fragment.for this we will use firebaseRecycleradapter for retrieving ...
Read more >
how to retrieve data from firebase in recyclerview android
Inside that column Navigate to Firebase Realtime Database. Click on that option and you will get to see two options on Connect app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found