1

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lotfi
  • 660
  • 1
  • 6
  • 21
  • possible duplicate https://stackoverflow.com/a/44035700/2809351 – Lalit Jadav Aug 24 '17 at 04:27
  • You want `View.GONE`, which removes the item from the view hierarchy (instead of just hiding its contents). See https://stackoverflow.com/questions/41223413/how-to-hide-an-item-from-recycler-view-on-a-particular-condition – Frank van Puffelen Aug 24 '17 at 05:34
  • Unrelated to your actual question, you might be able to reduce the data download by using a range query: `AllPostsQuery.orderByChild("type").startAt(1).endAt(6)`. You'll still need to filter in the code, but it may transfer less data. – Frank van Puffelen Aug 24 '17 at 05:36

0 Answers0