This question already has an answer here:
Why I cant use the populateViewHolder override method?
(1 answer)
Closed 5 years ago.
I'm attempting to view database information onto my android app using the FirebaseRecyclerAdapter but where I'm expecting the populateViewHolder method I'm getting two others popping up. The code is below:
FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(
NoteModel.class,
R.layout.single_note_layout,
NoteViewHolder.class,
fNotesDatabase
) {
#Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
#Override
protected void onBindViewHolder(#NonNull NoteViewHolder holder, int position, #NonNull NoteModel model) {
}
};
}
public NoteViewHolder and protected void onBindViewHolder were not expected as in the past after using FirebaseReccylerAdapater these two haven't appeared. I was expecting:
protected void populateViewHolder ( NoteViewHolder viewHolder, NoteModel model, int position) {
}
Anyone have any ideas why this is no longer available? I'm not sure how to go about completing the code as I've not seen it like this before. I need to add....
String noteId = getRef(position).getKey();
....but I don't know where.
Thanks in advance.
The FirebaseUI migration guide is a good place to start. Essentially, you'll want to do something like this:
FirebaseRecyclerOptions<NoteModel> options = new FirebaseRecyclerOptions.Builder<>()
.setQuery(fNotesDatabase, NoteModel.class)
.build();
FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(options) {
#Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NoteViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_note_layout, parent, false));
}
#Override
protected void onBindViewHolder(#NonNull NoteViewHolder holder,
int position,
#NonNull NoteModel model) {
String key = getSnapshots().getSnapshot(position).getKey();
holder.bind(...);
}
};
Related
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Data,MyViewHolder>adapter=new FirebaseRecyclerAdapter<Data, MyViewHolder> ("what to put here?") {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull Data model) {
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
}
I was trying to do a recycler view where in I can put the FirebaseRealtimeDatabase
You will need a FirebaseRecyclerOptions:
FirebaseRecyclerOptions <Data> options = new FirebaseRecyclerOptions.Builder <Data>().setQuery(query, Data.class).build();
Data : Is your class.
query : Is your DatabaseReference that reference, your data node that you want to populate in the recycler view.
And pass this options to the place that you are referring to:
//here
.... = new FirebaseRecyclerAdapter<Data, MyViewHolder> (options) {
I am trying to separate my database from my UI controller using ViewModel and prevent constant data usage. I have searched for solution for days now without any headway.
I am using Firebase RecyclerAdapter in my app and the code is working well, but I don't know how to implement a LiveData Observer with the RecyclerAdapter.
any sample app or snippet will be of great help to me.
Here is a sample code from my Fragment
private void fetchValues() {
Query query1 = mDatabaseReference;
FirebaseRecyclerOptions<Course> options = new FirebaseRecyclerOptions.Builder<Course>()
.setQuery(query1, new SnapshotParser<Course>() {
#NonNull
#Override
public Course parseSnapshot(#NonNull DataSnapshot snapshot) {
return new Course(snapshot.child("course_title").getValue().toString(),
snapshot.child("course_code").getValue().toString(),
snapshot.child("course_lecturer").getValue().toString());
}
})
.build();
mFirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Course, CourseViewHolder>(options){
#NonNull
#Override
public CourseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
mCourseItemBinding = CourseItemBinding.inflate(LayoutInflater.from(getActivity()), parent, false);
View view = mCourseItemBinding.getRoot();
return new CourseViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CourseViewHolder holder, int position, Course course) {
holder.bind(course);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
}
};
mRecyclerView.setAdapter(mFirebaseRecyclerAdapter);
}
I want to have a nested RecyclerView, which is filled by using two FirestoreRecyclerAdapters, that gather two different Requests (responseOne and responseTwo) from Firestore.
In the following the scenario in short words:
firstRecyclerView --> populate with FirestoreRecyclerAdapter<Post, PostHolder>
secondRecyclerView (nested in firstRecyclerView) --> populate with FirestoreRecyclerAdapter<Comment, CommentHolder>
Here is my code for this scenario:
firstRecyclerView = rootView.findViewById(R.id.posts_list_recyclerview);
firstAdapter = new FirestoreRecyclerAdapter<Post, PostHolder>(repsonseOne) {
#Override
public void onBindViewHolder(#NonNull PostHolder holder, int position, #NonNull Post model) {
holder.setPostText_to_UI(model.getText());
}
#NonNull
#Override
public PostHolder onCreateViewHolder(#NonNull ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.posts_item_cardview, group, false);
secondRecyclerView = view.findViewById(R.id.comments_list_recyclerview);
secondAdapter = new FirestoreRecyclerAdapter<Comment, CommentHolder>(repsonseTwo) {
#Override
protected void onBindViewHolder(#NonNull CommentHolder holder, int position, #NonNull Comment model) {
holder.setAuthorDisplayName_to_UI(model.getAuthorUserId());
}
#NonNull
#Override
public CommentHolder onCreateViewHolder(#NonNull ViewGroup group, int viewType) {
View view = LayoutInflater.from(view.getContext()).inflate(R.layout.posts_comments_item_cardview, group, false);
return new CommentHolder(view);
}
};
secondRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
secondRecyclerView.setItemAnimator(new DefaultItemAnimator());
secondRecyclerView.setAdapter(secondAdapter);
secondAdapter.notifyDataSetChanged();
return new PostHolder(view);
}
};
firstRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
firstRecyclerView.setItemAnimator(new DefaultItemAnimator());
firstRecyclerView.setAdapter(firstAdapter);
firstAdapter.notifyDataSetChanged();
The firstRecyclerView works well without any problems and shows all the data.
However, the secondRecyclerView does not initialize, and I saw in Debug View that these two methods were not started by the code:
public CommentHolder onCreateViewHolder()
protected void onBindViewHolder(#NonNull CommentHolder holder, int position, #NonNull Comment model)
What can I do to get these two FirestoreRecyclerAdapters running?
I'm try to retrieve datafrom database in my fragment onViewCreated method I have used FirebaseRecylerAdapter like below
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter;
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder {
View view;
public AllUsersViewHolder(View itemView) {
super(itemView);
view = itemView;
}
}
when I use firebaseRecyclerAdapter = new FirebaseRecyclerAdapter it wont give me the populateViewHolder method just give method like below
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>() {
#Override
protected void onBindViewHolder(AllUsersViewHolder holder, int position, AllUsers model) {
}
#Override
public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
Can someone please tell me why this is happening
The constructor of the Firebase...Adapter classes has changed in FirebaseUI version 3. Given this query:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.equalTo(name);
If you're using a version 3 or higher, you use FirebaseRecyclerOptions:
FirebaseRecyclerOptions<model_class_name> options =
new FirebaseRecyclerOptions.Builder<model_class_name>()
.setQuery(query, model_class_name.class)
.build();
Then declare a FirebaseRecyclerAdapter:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
add the variable options as above. It is the variable of the class FirebaseRecyclerOptions
Then to add items you have to use onBindViewHolder since its the latest version of firebase ui:
#Override
protected void onBindViewHolder(Holder holder, int position, model_class_name model) {
// Bind the class object to the holder
// ...
}
For more info check this: https://github.com/firebase/FirebaseUI-Android/tree/master/database
I'm try to retrieve datafrom database in my fragment onViewCreated method I have used FirebaseRecylerAdapter like below
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter;
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder {
View view;
public AllUsersViewHolder(View itemView) {
super(itemView);
view = itemView;
}
}
when I use firebaseRecyclerAdapter = new FirebaseRecyclerAdapter it wont give me the populateViewHolder method just give method like below
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>() {
#Override
protected void onBindViewHolder(AllUsersViewHolder holder, int position, AllUsers model) {
}
#Override
public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
Can someone please tell me why this is happening
The constructor of the Firebase...Adapter classes has changed in FirebaseUI version 3. Given this query:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.equalTo(name);
If you're using a version 3 or higher, you use FirebaseRecyclerOptions:
FirebaseRecyclerOptions<model_class_name> options =
new FirebaseRecyclerOptions.Builder<model_class_name>()
.setQuery(query, model_class_name.class)
.build();
Then declare a FirebaseRecyclerAdapter:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
add the variable options as above. It is the variable of the class FirebaseRecyclerOptions
Then to add items you have to use onBindViewHolder since its the latest version of firebase ui:
#Override
protected void onBindViewHolder(Holder holder, int position, model_class_name model) {
// Bind the class object to the holder
// ...
}
For more info check this: https://github.com/firebase/FirebaseUI-Android/tree/master/database