Reputation: 401
Hi so I have one Firebase FirebaseRecyclerAdapter
and in my Firebase project in my database I have over 30 values and in my app I want to display only the first x items.
I searched on internet but I didn't find how I could load only x items.
This is my code:
mAdapter = new FirebaseRecyclerAdapter<Post, Holder>(
Post.class,
R.layout.item_layout,
Holder.class,
mRef
){
@Override
public void onBindViewHolder(Holder viewHolder, int position) {
Post post = (Post) this.getItem(position);
((Holder) viewHolder).bind(post);
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view;
view = inflater.inflate(R.layout.item_layout , parent, false);
return new Holder(view);
}
}
This is my holder Class:
public static class EventsOldHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public EventsOldHolder(View itemView) {
}
public void bind(final Post post) {
}
@Override
public void onClick(View view) {
}
I know I can hide the elements when position has a higher value than x but if I have a lot of items and hide all of them my app will work slower and if you can help me with a better solution.
Upvotes: 0
Views: 143
Reputation: 138824
To solve this, you should pass to the FirebaseRecyclerAdapter
constructor a query
instead of the mRef
, which I assume is a DatabaseReference
object.
Assuming that you have a database structure that looks like this:
Firebase-root
|
--- posts
|
--- postId
|
--- title: "Post Title"
|
--- //Other details
To display only the first 5 items, the query should look like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("posts").orderByChild("title").limitToFirst(5);
query.addListenerForSingleValueEvent(/* ... */);
Upvotes: 1