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.