Reputation: 11
To simplify my problem: I have a Firebase database that has a "businesses" node and the businesses node has children with ID's and corresponding data about that particular business. I do not want to show all of the businesses in my firebase Recycler View, only certain ones. For the ones I want to show at any give time, I have a list of IDs.
...
List<String> businessList = ("00002", "00004", "00021");
...
private void setUpFirebaseAdapter() {
mFirebaseAdapter = new FirebaseRecyclerAdapter<User, FirebaseBusinessViewHolder>
(User.class, R.layout.business_list_item, FirebaseBusinessViewHolder.class, mBusinessReference{
@Override
protected void populateViewHolder(FirebaseBusinessViewHolder viewHolder, User model, int position){
for (String businessID : businessList) {
if (businessID.equals(model.getId())){
//show this item
viewHolder.bindBusiness(model, true);
}
if(!businessID.equals(model.getId())){
//don't show this item
}
}
}
};
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mFirebaseAdapter);
}
If I run this code, it will only show the data from the businessList, but it also leaves an empty space for the other items stored in the database whose ID was not on the businessList, as it creates the empty list item but does not bind any data to it. I obviously don't want the gap/empty space to show up for unwanted items. How can I adjust my list adapter/view holder to only show the desired items?
Upvotes: 1
Views: 1310
Reputation: 138824
You did not post what query you are using in your code, so i'm assuming that it's just a DatabaseReference
for everything what is beneath /businesses
. If you're doing that, you're going to get a call for the populateView()
method, for every child at that location. This means that you're going to be asked to make a view
for every child. You don't get to choose whether or not, a view will appear for that item.
It looks like you're assuming that your if statement
in populateView
, will skip over those items that not belong to your businessList
. But what's actually happening, is that you're simply allowing an empty view to occupy that spot in the list.
Instead, you should come up with a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria. You can use a equalTo()
method to achieve this.
Another solution it would be, to read the entire contents of the location, manually filter out the items you don't want, and build a list of items you do want. You can build an Adapter
with that list, and it can then become the input to a RecyclerView.
Hope it helps.
Upvotes: 1
Reputation: 598740
You could potentially completely hide the items that you don't want to show by setting their height to 0 or setting their visibility to gone. But that sounds like a horrible kludge, since you're in that case downloading data that isn't needed.
You have two options here:
FirebaseIndexedRecyclerAdapter
.ValueEventListener
as described in the Firebase documentationUpvotes: 2