I am developing a forum kind of app where users can post questions and tag them with related technologies.
The app has the modules and database similar to the Firebase database repo.
I wanted to filter the posts according to their tags. As filtering on server-side is not possible for multiple key values, I had to approach a way to filter it before populating in RecyclerView.
I referred a lot of questions here and found Puf's answer relevant for this case.
This is my JSON tree
"posts" : {
"-Kdb0BsybfpPtkAPRF1-" : {
"author" : "Anush Surendran",
"body" : "Test 1 ",
"commentCount" : 0,
"created" : 1487787053303,
"starCount" : 1,
"stars" : {
"v9KTwyI2DMbDgKHALgPSJEBK3fi1" : true
},
"tag" : "#CSS",
"title" : "Checking Post Time",
"uid" : "BvNgX2U5U4SriTWsoEtqbGYgOTw1"
},
"-Kdb2D9vD-3kHWpCMIFi" : {
"author" : "Anush Surendran",
"body" : "Abc",
"commentCount" : 0,
"created" : 1485000000000,
"starCount" : 0,
"tag" : "#HTML",
"title" : "Test 2",
"uid" : "BvNgX2U5U4SriTWsoEtqbGYgOTw1"
}
I am overriding parseSnapshot with the following logic
@Override
protected Post parseSnapshot(DataSnapshot snapshot) {
if (snapshot.getValue(Post.class).tag.equals("#HTML")){
Log.d(TAG,"gotHTML Filtered Posts");
return snapshot.getValue(Post.class);
}
else
return super.parseSnapshot(snapshot);
}
Trying with just one value now. I'll put this in a loop to filter multiple tags (say HTML, CSS, JS)
The code works fine and I could see the if condition work as expected.
I couldn't figure out what to return in the else part if there are no posts with specific tag. The current else part returns all posts.
Is there a better way to filter data as compared to this?
Any help would be greatly appreciated!