I can't find populateViewHolder Method in FirebaseRecyclerAdapter class
Solution 1
The syntax has changed. The section of the doc outlining the calls is here:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
Here is the app gradle file with Firebase Database and FirebaseUI versions:
// FirebaseUI for Firebase Realtime Database
compile 'com.firebaseui:firebase-ui-database:3.1.0'
compile 'com.google.firebase:firebase-database:11.4.2'
I have standard ViewHolder and Model classes defined. I'll leave them out for brevity.
Instance variables at top of my Fragment class:
private RecyclerView mPeopleRV;
private FirebaseRecyclerAdapter<PersonModel, PeopleVH> mPeopleRVAdapter;
In onCreateView() of my Fragment:
mPeopleRV = v.findViewById(R.id.peopleRV);
In onActivityStarted() of my Fragment is where all the action is.
onCreateViewHolder is where the row layout gets inflated. And bindViewHolder() is where you populate the row with your model data fetched from Firebase.
The constructor for the adapter now takes a set of options that you define based on your query and model.
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("People");
Query personsQuery = personsRef.orderByKey();
mPeopleRV.hasFixedSize();
mPeopleRV.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<PersonModel>().setQuery(personsQuery, PersonModel.class).build();
mPeopleRVAdapter = new FirebaseRecyclerAdapter<PersonModel, PeopleVH>(personsOptions) {
@Override
protected void onBindViewHolder(PeopleVH holder, int position, PersonModel model) {
holder.setPersonFirstName(model.getFirstName());
holder.setPersonLastName(model.getLastName());
}
@Override
public PeopleVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_row_people_rv, parent, false);
return new PeopleVH(view);
}
};
mPeopleRV.setAdapter(mPeopleRVAdapter);
Added later:
Don't forget to start and stop the adapter listener in onStart() and onStop():
@Override
public void onStart() {
super.onStart();
mPeopleRVAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mPeopleRVAdapter.stopListening();
}
Solution 2
In addition to @Lucy's answer. Now, there are architecture component lifecycle owners. As per the firestoreui android doc
If you don't want to manually start/stop listening you can use Android Architecture Components to automatically manage the lifecycle of the FirestoreRecyclerAdapter
. Pass a LifecycleOwner
to FirestoreRecyclerOptions.Builder.setLifecycleOwner(...)
and FirebaseUI will automatically start and stop listening in onStart()
and onStop()
.
In this specific situation,personsOptions.setLifecycleOwner(myFragmentOrActivity.this)
Mohamed AbdelraZek
Experienced Mobile Developer with a demonstrated history of working in the computer software industry. Skilled in Android Development, Java, Kotlin and Swift. Strong engineering professional with a Bachelor of Computer Science focused on Computer Science.
Updated on August 08, 2022Comments
-
Mohamed AbdelraZek over 1 year
I want to use
FirebaseRecyclerAdapter
in my project and I used this code segment in a previous ProjectFirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder> adapter = new FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, ref) { public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage, int position) { chatMessageViewHolder.nameText.setText(chatMessage.getName()); chatMessageViewHolder.messageText.setText(chatMessage.getMessage()); } }; recycler.setAdapter(mAdapter);
and it was working fine but now it doesn't work any more, Is there any update in the new version of
FirebaseUi
that can't allow me to reuse this code now?I tried this in my project
@Override protected void onStart() { super.onStart(); FirebaseRecyclerAdapter<BlogModel, BlogListViewHolder> adapter = new FirebaseRecyclerAdapter<BlogModel, BlogListViewHolder>(BlogModel.class, android.R.layout.two_line_list_item, BlogListViewHolder.class, mDatabaseReference) { public void populateViewHolder(BlogListViewHolder blogListViewHolder, BlogModel blogModel, int position) { blogListViewHolder.postTitle.setText(blogModel.getPostTitle()); blogListViewHolder.postDesc.setText(blogModel.getPostDesc()); } }; mBlogList.setAdapter(adapter); }
and Android Studio gives an error saying you must implement
onCreateViewHolder,onBindViewHolder
Method.BlogListViewHolder class
public static class BlogListViewHolder extends RecyclerView.ViewHolder { ImageView postImage; TextView postTitle; TextView postDesc; public BlogListViewHolder(View view) { super(view); postImage = view.findViewById(R.id.blog_image_id); postTitle = view.findViewById(R.id.blog_title_id); postDesc = view.findViewById(R.id.blog_desc_id); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } }
I use this Dependencies
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.0.2' compile 'com.google.firebase:firebase-auth:11.4.2' compile 'com.google.firebase:firebase-database:11.4.2' compile 'com.google.firebase:firebase-storage:11.4.2' compile 'com.firebaseui:firebase-ui-database:3.1.0' compile 'com.firebaseui:firebase-ui-firestore:3.1.0' compile 'com.firebaseui:firebase-ui-auth:3.1.0' compile 'com.firebaseui:firebase-ui-storage:3.1.0' testCompile 'junit:junit:4.12' compile 'com.squareup.picasso:picasso:2.5.2' } apply plugin: 'com.google.gms.google-services'