I need to filter childs of a Firebase Database upon an (int) Type that must be equal to 1, 2 and 6. It is not possible to do it with NoSQL filtering of Firebase
So I will take all the records and tell the adapter to not all of display them
my code is based on the database example of Firebase and modify inherited class
public class AllPostsFragment extends PostListFragment {
this methode will take all the records (Posts)
@Override
public Query getQuery(DatabaseReference databaseReference) {
Query AllPostsQuery = databaseReference.child("posts");
// I can not have multiple equal : Query AllPostsQuery = databaseReference.child("posts").orderByChild("type").equalTo(5);
return AllPostsQuery;
}
then the adapter
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Set up Layout Manager, reverse layout
mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
mRecycler.setLayoutManager(mManager);
// Set up FirebaseRecyclerAdapter with the Query
Query postsQuery = getQuery(mDatabase);
mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post,
PostViewHolder.class, postsQuery) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position) {
final DatabaseReference postRef = getRef(position);
if ((model.type!=1)&& (model.type!=2) && (model.type!=6)) viewHolder.itemView.setVisibility(View.GONE);
else viewHolder.itemView.setVisibility(View.VISIBLE);
// Set click listener for the whole post view
final String postKey = postRef.getKey();
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Launch PostDetailActivity .... ....
});
}
};
mRecycler.setAdapter(mAdapter);
}
The setVisibility works but there are still empty space displayed by the adapter for other unwanted records