1

I have a recyclerview I want to display post in recycler view ..what my problem is I want to display only posts that as popularlist="true" in the database...there are photo that I don't want to displayenter image description here please take a look at database structure ..over here popularlist="false" by default by all photo I want the recycler view to check if the popularlist="true" it needs to display in recycler view if its falseit should not display the photo ....can any one please tell me how can I do this ...

      @Override
public int getItemViewType ( int position ) {
    int viewType;
    if (moviesList.get(position).getType_post().contains("Photo") ) {

            viewType = IMAGE_TYPE;
    } else if(moviesList.get(position).getType_post().contains("Video")){
        viewType = VIDEO_TYPE;
    }
    return viewType;
}

I used this code for checking photo or video ...I don't know about filtering

what I have tried I created an interface as listviewmodel below

    interface ListViewModel {
    int VIEW_TYPE_PHOTO = 1;
    int VIEW_TYPE_VIDEO = 2;
    int getViewType();

   }

and created 2 model class called photoviewmodel and videoviewmodel which extends listviewmodel

   class PhotoViewModel implements ListViewModel{

String image_path;

public PhotoViewModel(String image_path) {
    this.image_path = image_path;
}
@Override
public int getViewType() {
    return VIEW_TYPE_VIDEO;
}
}

in fragment where I get photo I add this code where recycler view is in

       List<ListViewModel> listViewModels = new ArrayList<>();
                for (Photo photo : photos) {
                    if(photo.getPopularlist().contains("true")){
                        if(photo.getType_post().equals("Photo")){
                            List<PhotoViewModel> pho = new ArrayList<>();
                            PhotoViewModel p = new PhotoViewModel();
                            p.setImagePath(photo.getImage_path());
                            pho.add(p);

                            listViewModels.add(new PhotoViewModel(photo.getImage_path(),photo.getDescription(),photo.getDate_created()));
                           // Log.d(TAG, "onDataChange: alphs"+photoViewModel1.getImagePath());

                        }
                        if(photo.getType_post().equals("Video")){
                            listViewModels.add(new VideoViewModel(photo.getImage_path()));
                        }
                    }
                }

it display the body but I am not getting image path and other thing which I have add in fragment over here

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
new learner
  • 111
  • 2
  • 12
  • Please share your code... In major terms, you need to create an Adapter with the item list data. Before sending all data to Adapter just check for popularlist var – Edu Romero Jul 17 '18 at 11:32

1 Answers1

1

You need to add a query:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Query query=ref.orderByChild("popularlist").equalTo(true);

Then you will be able to retrieve the data that only has popularlist=true

https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • can you tell me where should I add this,,...should I add this in adapter?? – new learner Jul 17 '18 at 11:43
  • Since you are using Firebase, then use FirebaseRecyclerAdapter since it is easier https://stackoverflow.com/questions/49585134/how-to-use-firebaserecycleradapter and https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849 – Peter Haddad Jul 17 '18 at 11:50
  • bro thanks for you answer but I have two view type video and photo ... bro does this filter out other photos which don't have **popularlist="false"** – new learner Jul 17 '18 at 11:54