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?
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);
}
reference = FirebaseDatabase.getInstance().getReference().child("Order");
FirebaseRecyclerOptions<Order1> options = new FirebaseRecyclerOptions.Builder<Order1>()
.setQuery(reference.child(CurrentUser).child("Items").child("1553778276400"), Order1.class).build();
FirebaseRecyclerAdapter<Order1, OrderDeatilsViewHolder> adapter = new FirebaseRecyclerAdapter<Order1, OrderDeatilsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final OrderDeatilsViewHolder holder, int position, #NonNull final Order1 model) {
holder.proname.setText(model.getPname());
holder.proquantity.setText(model.getQuantity());
holder.proprice.setText(model.getPrice());
holder.prototal.setText(model.getTootlamount());
Picasso.get().load(model.getImage()).into(holder.imageView);
}
#NonNull
#Override
public OrderDeatilsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orderdetailslayout, parent ,false);
OrderDeatilsViewHolder holder = new OrderDeatilsViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();[enter image description here][1]
I try to get data but it show error like "Can't convert object of type java.lang.String to type com.example.home.Model.Order1"
Do this :
#Override
protected void onBindViewHolder(#NonNull final OrderDeatilsViewHolder holder, int position) {
holder.proname.setText(model.get(position).getPname());
holder.proquantity.setText(model.get(position).getQuantity());
holder.proprice.setText(model.get(position).getPrice());
holder.prototal.setText(model.get(position).getTootlamount());
Picasso.get().load(model.get(position).getImage()).into(holder.imageView);
}
I want to read the value of a child(name) from Client's nodes when I use a firebase recycler adapter from Request's nodes which have childs with the value of Client's keys
I tried this, but I only received the link
adapter=new FirebaseRecyclerAdapter<Request, OrderViewHolder>(optionsReq) {
#Override
protected void onBindViewHolder(#NonNull OrderViewHolder holder, final int position, #NonNull final Request model) {
holder.txtOrderId.setText(adapter.getRef(position).getKey());
holder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
holder.txtOrderAddress.setText(model.getAddress());
holder.txtOrderDate.setText(Common.getDate(Long.parseLong(model.getDate())));
holder.txtOrderClient.setText(client.child(model.getDniclient()).child("name").toString());
#NonNull
#Override
public OrderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent,false);
return new OrderViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
DatabaseReference databaseReference;
String name;
adapter=new FirebaseRecyclerAdapter<Request, OrderViewHolder>(optionsReq) {
#Override
protected void onBindViewHolder(#NonNull OrderViewHolder holder, final int position, #NonNull final Request model) {
holder.txtOrderId.setText(adapter.getRef(position).getKey());
holder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
holder.txtOrderAddress.setText(model.getAddress());
holder.txtOrderDate.setText(Common.getDate(Long.parseLong(model.getDate())));
holder.txtOrderClient.setText(client.child(model.getDniclient()).child("name").toString());
databaseReference = FirebaseDatabase.getInstance().getReference().child("Client").child(model.getDniclient());
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
name = dataSnapshot.child("name").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
#NonNull
#Override
public OrderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent,false);
return new OrderViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
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(...);
}
};