Reputation: 103
I want to order my posts in the FirebaseRecyclerView by the String Votes. It contains a number as a String. If I write orderbychild("Votes")
it shows up the smallest number first.
How can I turn that around? Cheers!
Upvotes: 3
Views: 4802
Reputation: 291
I would like to add just one more thing to @Ari approch.
private Query getQuery() {
Query query;
if(last_value == -1) {
query = getPostViewCountRef()
.orderByChild("negativeViewsCount")
.limitToFirst(ITEMS_PER_PAGE);
}else {
query = getPostViewCountRef()
.orderByChild("negativeViewsCount")
.startAfter(last_value)
.limitToFirst(ITEMS_PER_PAGE);
}
return query;
}
as you can see we limitToFist instead of limitTolast
Upvotes: 0
Reputation: 37
the code below is how I fixed the issue. it uses an 3 integers - a max value the max value is equal to what ever you want with in the bounds of the datatype restrictions.
int round = 5;
int roundKills = 1000;
int points = 500;
round = round-99999;
roundKills = roundKills-999999;
points = points-99999999;
String oundkills = Integer.toString(roundKills);
String oints = Integer.toString(points);
String ound = Integer.toString(round);
LeaderBoardInput leaderBoardInput = new
LeaderBoardInput(user,"SM",oundkills,oints,ound);
the LeaderboardInput class is just getters and setters for values that are being passed to the database
reference.child(user+System.currentTimeMillis()).setValue(leaderBoardInput);
when you pull the value, you would do something like the following:
round = round+99999;
roundKills = roundKills+999999;
points = points+99999999;
for your example if you have 5 votes and a max vote of 9,999,999 the code would look like this
int votes = 5;
long invertVotes = votes - 9999999;
String dbVotes = Integer.toString(invertVotes);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("scores");
reference.child("Votes"+System.currentTimeMillis()).setValue(dbVotes);
Query reference = FirebaseDatabase.getInstance()
.getReference().child("Votes").orderByChild("Votes");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//parse data to recycler view adapter and call //notifyDatasetChange()
Iterable<DataSnapshot> children = dataSnapshot.getChildren();
for (DataSnapshot child : children) {
Votes voteValues = child.getValue(voteinput.class);
int value = votevalues.getvotes()+9999999;
scores.add(new Votes( value));
}
getdb = false;
}
});
with Votes be a getter setter class and the scores variable being a list of the Votes class instances this will order votes 9999994 in db but print 5 to list out put of multiple values will 5 4 3 2 1 instead of 1 2 3 4 5
Upvotes: 0
Reputation: 7556
A quick and rather flexible way to do this on server side is to add an extra field and index your data both with Votes
and negativeVotes
, where negativeVotes=-Votes
Then for reverse order you do this:
orderbychild("negativeVotes")
And for regular order you do this:
orderbychild("Votes")
Upvotes: 3
Reputation: 6007
A way that I think better is ordering in client-side using reverse().
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
Upvotes: 1
Reputation: 28750
You don't necessarily turn it around.
Let's say your data looks like this:
{
"polls": {
"id1": { "name": "lorem", "votes": 4 }
"id2": { "name": "ipsum", "votes": 3 }
"id3": { "name": "dolor", "votes": 5 }
"id4": { "name": "sit", "votes": 2 }
"id5": { "name": "amot", "votes": 6 }
}
}
If for instance you need the top 4 you would do this:
.orderByChild("Votes").limitToFirst(4)
Would return:
{
"id4": { "name": "sit", "votes": 2 }
"id2": { "name": "ipsum", "votes": 3 }
"id1": { "name": "lorem", "votes": 4 }
"id3": { "name": "dolor", "votes": 5 }
}
If you want the other end, you would do this instead:
.orderByChild("Votes").limitToLast(4)
And the return would be:
{
"id2": { "name": "ipsum", "votes": 3 }
"id1": { "name": "lorem", "votes": 4 }
"id3": { "name": "dolor", "votes": 5 }
"id5": { "name": "amot", "votes": 6 }
}
If you need all of them, just order in the ui.
Upvotes: 2