1

I have a problem with my RecyclerView. In summary, I put a RecyclerView connected to my database. Then when I start the activity the list is empty, but if I go back to the last activity and I re-open this activity the list is filled.

Here is the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_followers);

    mAuth = FirebaseAuth.getInstance();
    if(mAuth == null){
        startActivity(new Intent(FollowersActivity.this, HomeScreenActivity.class));
        finishAffinity();
    }

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Seguidores");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Usuarios");
    requestDB = FirebaseDatabase.getInstance().getReference().child("requesting");

    requestList = findViewById(R.id.requestList);
    requestList.setHasFixedSize(true);
    requestList.setLayoutManager(new LinearLayoutManager(this));
}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Users, requestViewHolder> requestAdapter = new FirebaseRecyclerAdapter<Users, requestViewHolder>(

            Users.class,
            R.layout.users_row_layout,
            requestViewHolder.class,
            requestDB.child(mAuth.getCurrentUser().getUid())

    ) {
        @Override
        protected void populateViewHolder(final requestViewHolder viewHolder, Users model, int position) {
            final String other_userId = getRef(position).getKey();

            mDatabase.child(other_userId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    String name = dataSnapshot.child("nombre").getValue().toString();
                    String username = dataSnapshot.child("usuario").getValue().toString().substring(1);
                    String fotoPath = dataSnapshot.child("foto").getValue().toString();

                    viewHolder.nameTextView.setText(name);
                    viewHolder.usernameTextView.setText(username);
                    Glide.with(getApplicationContext()).load(fotoPath).into(viewHolder.imageView);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
    };

    requestList.setAdapter(requestAdapter);
}

public static class requestViewHolder extends RecyclerView.ViewHolder {

    View mView;
    CircleImageView imageView;
    TextView nameTextView, usernameTextView;

    public requestViewHolder (View itemView){
        super(itemView);
        mView = itemView;

        imageView = itemView.findViewById(R.id.circleImageView);
        nameTextView = itemView.findViewById(R.id.nameTextView);
        usernameTextView = itemView.findViewById(R.id.usernameTextView);
    }
}

Then I realized that the problem is with the database, because if I change the root the list is filled in the first time.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • So what is your question now? If you have solved the problem already then please remove the question. Thanks. – Reaz Murshed Apr 22 '18 at 06:16
  • The problem is that I have to re-open the activity. I didn't fix it, I thought it was the database – Nicolas Villar Apr 22 '18 at 16:17
  • You need to remove `requestList.setHasFixedSize(true);` from your code. – Reaz Murshed Apr 22 '18 at 16:32
  • Great to know that helped. I am putting this as an answer. – Reaz Murshed Apr 23 '18 at 19:10
  • You are using a very old version of `FirebaseRecyclerAdapter` class which has the constructor with 4 argument. Use the latest version. So **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Apr 23 '18 at 19:38

1 Answers1

0

You need to remove requestList.setHasFixedSize(true); from your code.

You might consider using setHasFixedSize in case of your RecyclerView items will not be changing their heights during runtime. It has some extra benefits like optimizing the whole recycling of the views. However, you cannot use it for loading dynamic RecyclerView.

See the documentation here for better understanding.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98