Reputation: 71
I am working on a simple app where you can retrieve video in viewpager not list view and user can also like video.
But now I want to sort all the videos on the basis of most liked.
Here is my database screenshot -
and her is the code of which I made a video adapter from which I retrieve video
code -
videoView.setVideoPath(obj.getUrl());
title.setText(obj.getTitle());
desc.setText(obj.getDesc());
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
pbar.setVisibility(View.GONE);
mediaPlayer.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
}
Update: Here is my updated database screenshot:
~
Upvotes: 0
Views: 258
Reputation: 598797
Firebase Realtime Database queries can only sort data on values that are stored in the database. There is no way to order on something that is calculated, like the number of child elements under the Likes/$postid
node in your case.
The common workaround is to store the number of likes in the database too, for example as a LikeCount
property, and then order on that.
You can securely have the client write its like/unlike and increment/decrement the counter by:
increment()
operation to perform the change atomically.For an example of this, see my answer here: Using Firebase Security Rules, how can I secure a node that has transactions run on it?
Once you have stored the the LikeCount
you can sort on it by using a query. Something like:
DatabaseReference postsRef = FirebaseDatabase.getInstance().getReference("Posts");
Query query = postsRef.orderBy("LikeCount");
query.addValueEventListener(...
Upvotes: 1