I have a database of 800+ items and I want to filter them by rarity. With "single" rarity (e.g only "legendary" rarity) I can easily do this, but the problem appears when trying to combine two rarities (e.g "event & basic" rarity).
This is the method I use:
private void loadFirebaseData(String rarity1, String rarity2) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Outfits");
reference.keepSynced(true);
Query query = reference.orderByChild("rarity").startAt(rarity1).endAt(rarity2);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrayCosmetics = new ArrayList<>();
for (DataSnapshot data : dataSnapshot.getChildren()) {
Cosmetics cosmetics = data.getValue(Cosmetics.class);
arrayCosmetics.add(cosmetics);
}
cosmeticsAdapter = new CosmeticsAdapter(getActivity(), arrayCosmetics);
cosmeticsAdapter.setHasStableIds(true);
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setAdapter(cosmeticsAdapter);
Collections.shuffle(arrayCosmetics, new Random(3));
rootView.findViewById(R.id.progressBar).setVisibility(View.GONE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
And calling it like: loadFirebaseData("basic", "event");
So the issue is that it loads me all the rarities from alphabetically "b" all the way to "e" (e.g "basic", "classic", "common", "elite", "epic") and obviously I want only "basic" and "event"
The database is structured like this:
"01" : {
"available" : "Wanderer Crate",
"created" : "20191023T021358Z",
"id" : 11010006,
"image" : "https://prod-live-front.playbattlegrounds.com/images/item_176/11010006.png",
"info" : "Drop Chance: 5.00%",
"marketable" : true,
"name" : "Dirty Tank Top (White)",
"rarity" : "common",
"slot" : "torso" }
"02" : {
"available" : "Wanderer Crate",
"created" : "20191023T021358Z",
"id" : 11010014,
"image" : "https://prod-live-front.playbattlegrounds.com/images/item_176/11010014.png",
"info" : "Drop Chance: 5.00%",
"marketable" : true,
"name" : "Tank Top (Charcoal)",
"rarity" : "common",
"slot" : "torso" }
Firebase rules:
"Outfits": {
".indexOn": ["rarity"]
}
It is possible to achieve what I want without loading the whole database in one big ArrayList<>()
?
If I have the whole ArrayList<>()
I can do something like this:
private void rarityFilter(String rarity1, String rarity2) {
ArrayList<Cosmetics> newListCosmetics = new ArrayList<>();
for (Cosmetics cosmetics : arrayCosmetics) {
String name = cosmetics.getRarity().toLowerCase();
if (name.contains(rarity1) || name.contains(rarity2))
newListCosmetics.add(cosmetics);
}
cosmeticsAdapter.setFilter(newListCosmetics);
}
But isn't it bad for performance to load the whole ArrayList<>()
?