FirebaseRecyclerAdapter - populateViewHolder is not populating the data for the first time it runs but when I closed the app and opened it, the data is binded in RecyclerView View Holder. I am not getting why data is not populating for first time, it is showing blank screen
One more observation if I am debugging the app and wait at the debug point on Query for 2-3 seconds populateViewHolder is called and the view displays .. No idea why this is happening
Main Activity
mDatabase = FirebaseDatabase.getInstance().getReference();
// [END create_database_reference]
mRecycler = (RecyclerView) findViewById(R.id.recycler_view);
mRecycler.setHasFixedSize(true);
setSupportActionBar(toolbar);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mRecycler.setLayoutManager(layoutManager);
// Set up FirebaseRecyclerAdapter with the Query
//Query postsQuery = mDatabase.child("EN").child("Courses") ;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("EN").child("Courses");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(MainActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
}
});
Log.d("ref", String.valueOf(ref));
// Log.d("Query", String.valueOf(postsQuery));
mAdapter = new FirebaseRecyclerAdapter<Course, CourseViewHolder>(Course.class, R.layout.item_home,
CourseViewHolder.class, ref) {
@Override
protected void populateViewHolder(final CourseViewHolder viewHolder, final Course model, final int position) {
viewHolder.bindToPost(model);
}
};
mRecycler.setAdapter(mAdapter);`
ViewHolder
`public class CourseViewHolder extends RecyclerView.ViewHolder {
public TextView titleView;
public TextView descriptionView;
public ImageView IconView;
public TextView lessonCountView;
public CourseViewHolder(View itemView) {
super(itemView);
titleView = (TextView) itemView.findViewById(R.id.title);
descriptionView = (TextView) itemView.findViewById(R.id.description);
//IconView = (ImageView) itemView.findViewById(R.id.star);
}
public void bindToPost(Course post) {
// Log.d("Post", String.valueOf(post));
titleView.setText(post.getName());
descriptionView.setText(post.getDescription());
}
}`
My Model Pojo
@IgnoreExtraProperties
public class Course {
public String description;
public String title;
public Course() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Course(String title, String description) {
// this.description = author;
this.title = title;
this.description =description;
}
public String getName() {
return title;
}
public String getDescription() {
return description;
}
}
Are you setting a LayoutManager for your RecyclerView? I don't see that in the provided code.
@samtstern yes, it's in main Activity . first few lines
final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mRecycler.setLayoutManager(layoutManager);
@samtstern here is what exactly happening.
After Successfully LoginActivity Coming to Main Activity, Then in Main Activity the data is fetched from Firebase Database, But After successful login activity coming back to main activity populateViewHolder() isn't getting executed in a FirebaseRecyclerAdapter for the first time when app runs, but when i close the app and reopened populateViewHolder() is getting executed.
It only happening after going from LoginActivity to MainActivity,
If just come to main activity or OnClick then Intent activity everything is working fine there.
And i am not using FirebaseAuthUi for Authentication.
Tested with all different scenarios, It might be a BUG. Please do confirm
Steps to Reproduce
1)Implement Login with FirebaseAuth (No FirebaseAuthUI used and sign in method Google Sign IN)
2)After Successful Login in Login Activity, Launch Main Activity through Intent
3)Then in Main Activity the data is fetched from Firebase Database, and use FirebaseDatabaseUI and RecyclerView with a Custom View Adapter.
you can notice After successful login activity coming back to main activity populateViewHolder() isn't getting executed in a FirebaseRecyclerAdapter for the first time when app runs, but when i close the app and reopened populateViewHolder() is getting executed.
Hmmm.... could this be a race condition with getting the access token?
On Thu, Sep 1, 2016 at 11:03 AM Prashanth Reddy [email protected]
wrote:
Tested with all different scenarios, It might be a BUG. Please do confirm
Steps to Reproduce
1)Implement Login with FirebaseAuth (No FirebaseAuthUI used and sign in
method Google Sign IN)
2)After Successful Login in Login Activity, Launch Main Activity through
Intent
3)Then in Main Activity the data is fetched from Firebase Database, and
use FirebaseDatabaseUI and RecyclerView with a Custom View Adapter.
you can notice After successful login activity coming back to main
activity populateViewHolder() isn't getting executed in a
FirebaseRecyclerAdapter for the first time when app runs, but when i close
the app and reopened populateViewHolder() is getting executed.—
You are receiving this because you commented.Reply to this email directly, view it on GitHub
https://github.com/firebase/FirebaseUI-Android/issues/279#issuecomment-244161550,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA3w3xajZjgHvOjxhcKlbu3WyMzG47hiks5qlxNfgaJpZM4Jy3yC
.
Yes, Might be, @puf please confirm me it after reproducing from your side, and please let me know if you have any workaround for this problem.
According to Prashanth's code snippet he is setting the layout manager in
the last line of code:
final LinearLayoutManager layoutManager = new
LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mRecycler.setLayoutManager(layoutManager);
On Sep 1, 2016 11:13 PM, "Sam Stern" [email protected] wrote:
Are you setting a LayoutManager for your RecyclerView? I don't see that
in the provided code.—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/firebase/FirebaseUI-Android/issues/279#issuecomment-244141765,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADqGeyhcWFcjK_unhwXYsIVY46Xo5Vayks5qlwMVgaJpZM4Jy3yC
.
Yes @tremerhl Any workaround for this problem is appreciated. ASAP Please.
@ItsPR Thanks for the detailed report. It might indeed be a bug in FirebaseUI. If it is, the most helpful thing you can do now is find the cause of the problem and provide a PR that fixes it. While we may sometimes be a bit slow to accept PRs that add features (mostly due to the cross-platform nature of FirebaseUI), you'll see that we're quite fast about merging small bug fixes.
You can also wait for somebody else to have time to reproduce the problem or run into it and fix it for you. At the moment neither of those has happened, apparently not enough people are running into this or are blocked by it. If you need a fix soon, your best recourse is to work on finding the cause and submitting a PR. That's one of the reasons we released FirebaseUI as open-source, so that you don't have to necessarily wait for us to fix a problem that is blocking for you.
The Problem is RecyclerView had a height of WrapContent , So Please Make Sure your recycler Height is set to Match_Parent. This will fix this issue.
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
The same bug happened with me. I have the same view logic as @itsPR. Was solved using his solution, applying android:layout_height="match_parent" to my RecyclerView.
So, in my oppinion this is a bug.
@filipebezerra @itsPR glad you both found the source of your issue! I don't believe this is a bug with FirebaseUI since it has to do with more basic RecyclerView layout. I am going to close this issue.
If you believe there is a change we can make in FirebaseUI to improve this experience, please re-open this issue to discuss.
I had similarly bug I spent a lot of time debugging it
@mulwa if you don't want to use android:layout_height="match_parent" to you RecyclerView. then make sure you're not using setFixedLayout(true) to fix this issue
The Problem is RecyclerView had a height of WrapContent , So Please Make Sure your recycler Height is set to Match_Parent. This will fix this issue.
<android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" />
The Firebase team documentation should be stated this as well :D It also solves my same issue.
try this way for your code
FirebaseRecyclerOptions options=
new FirebaseRecyclerOptions.Builder()
.setQuery(mIncomeDatabase,Data.class)
.setLifecycleOwner(this)
.build();
FirebaseRecyclerAdapter<Data, MyViewHolder> firebaseRecyclelerAdapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data,parent,false));
}
@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Data model) {
holder.setName(model.getName());
holder.setAmount(model.getAmount());
holder.setType(model.getType());
holder.setNote(model.getNote());
holder.setAmount(model.getAmount());
holder.setDate(model.getDate());
}
};
recyclerView.setAdapter(firebaseRecyclelerAdapter);
Most helpful comment
The Problem is RecyclerView had a height of WrapContent , So Please Make Sure your recycler Height is set to Match_Parent. This will fix this issue.