3

I have a firebase data structure like below. I'm trying to load this data with a FirebaseRecyclerAdapter. The issue is that the populateViewHolder() does not get called. The reference to the database structure works.

I can read the data with a friendsRef.addValueEventListener. What I like to avoid with the FirebaseRecyclerAdapter.(Code below)

The data structure is:

-users  
  |-<uid>
     |-friends
         |-<uid>:name1
         |-<uid>:name2

Authentication call in the main Activity

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(getString(R.string.logtag), "signInWithCredential:onComplete:" + task.isSuccessful());

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
//                            Log.w(TAG, "signInWithCredential", task.getException());
                            Toast.makeText(HomeActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        // ...
                    }
                });
    }

Fragment Code:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    View view = inflater.inflate(R.layout.friends_fragment, container, false);
    RecyclerView friendRecyclerView = (RecyclerView) view.findViewById(R.id.friends_recycler_view);

    friendRecyclerView.setHasFixedSize(true);
    friendRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));

    DatabaseReference friendsRef = mActivity.getRootRef().child("users").child(mActivity.getAuth().getCurrentUser().getUid()).child("friends");

    FirebaseRecyclerAdapter<String, ViewHolder> recyclerViewAdapter = new FirebaseRecyclerAdapter<String, ViewHolder>(String.class, R.layout.friends_list_item, ViewHolder.class,  friendsRef) {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, String model, int position) {
            viewHolder.friendName.setText(model);
        }
    } ;

    friendRecyclerView.setAdapter(recyclerViewAdapter);

    return view;
}

@Override
protected int getContentView() {
    return R.layout.friends_fragment;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    private TextView friendName;
    public ViewHolder(View itemView) {
        super(itemView);
        friendName=(TextView) itemView.findViewById(R.id.friends_name_list_item);
    }


}

friendsRef.addValueEventListener:

DatabaseReference friendsRef = mActivity.getRootRef().child("users").child(mActivity.getAuth().getCurrentUser().getUid()).child("friends");

        friendsRef.addValueEventListener(new ValueEventListener() {
                                             @Override
                                             public void onDataChange(DataSnapshot dataSnapshot) {

                                                 for (DataSnapshot data : dataSnapshot.getChildren()) {

                                                     Log.d("tag", data.toString());
                                                 }


                                             }
shalama
  • 1,133
  • 1
  • 11
  • 25
  • Are you certain your code has read access to the data you are trying to show? By default the Firebase Database is only readably by authenticated users and your code doesn't seem to authenticate the user. See the first blue box on the [Firebase documentation on reading data](https://firebase.google.com/docs/database/android/retrieve-data). – Frank van Puffelen Jun 17 '16 at 14:36
  • 1
    Yes it authenticates. Will add the code to the original post. – shalama Jun 17 '16 at 14:54
  • I am facing the same problem now. Did you find a solution? – IgorGanapolsky Dec 27 '16 at 19:24
  • change String model to HashMap model and then try again. – ugur Jan 10 '17 at 19:06

1 Answers1

0

Just change the layout_height of your RecyclerView to "match_parent" in your xml

Dagem.A
  • 152
  • 1
  • 7
  • Can you please expand on your answer. Perhaps include why that is causing the problem and how your solutions will fix it. This helps future readers understand the solution better. – Newd Jan 10 '17 at 20:10
  • Didn't fix anything for me, and I have no idea why it would. – StackAttack Nov 18 '17 at 19:41