How to code the view holder binding when using FirebaseRecyclerAdapter - android

#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) {

Related

How to implement ViewModel with FirebaseRecyclerAdapter

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);
}

The error is inn the firebaseRecyclerAdapter view adapter section

the error message is : incompatible types: FirestoreRecyclerOptions cannot be converted to FirebaseRecyclerOptions ----
and the code is as follows.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agencies);
firebaseFirestore= FirebaseFirestore.getInstance();
//views definition
agencyFeed = (RecyclerView)findViewById(R.id.agencyRecyclerView);
//------Requirements for retrieving data and storing it to recycler view---
//Query ----- for retrieving data from firestore
Query query = firebaseFirestore.collection("Agencies");
// recyclerOptions
FirestoreRecyclerOptions<AgencyModel> options = new FirestoreRecyclerOptions.Builder<AgencyModel>()
.setQuery(query,AgencyModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){
#NonNull
#Override
public AgencyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.agency_cards,parent,false);
return new AgencyViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull AgencyViewHolder holder, int position, #NonNull AgencyModel model) {
holder.name.setText(model.getName());
holder.location.setText(model.getLocation());
}
};
agencyFeed.hasFixedSize();
agencyFeed.setLayoutManager(new GridLayoutManager(this,2));
agencyFeed.setAdapter(adapter);
}
//viewholder class
public class AgencyViewHolder extends RecyclerView.ViewHolder {
private TextView name, location;
public AgencyViewHolder(#NonNull View itemView) {
super(itemView);
name= itemView.findViewById(R.id.agency_name); //name of agency
location= itemView.findViewById(R.id.locationTextView);
}
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
}
You are using Firestore according to your query, therefore you have to use FirestoreRecyclerAdapter and not FirebaseRecyclerAdapter. Therefore change:
adapter = new FirebaseRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){
into this:
adapter = new FirestoreRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){

Nested RecyclerView with two FirestoreRecyclerAdapters

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?

How to read the value of a "child" inside a FirebaseRecyclerAdapter?

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);
}

Cannot apply FirebaseRecyclerAdapter? [duplicate]

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

Categories

Resources