Reputation: 315
Inside MainActivity
, I have this function:
public void sortingData() {
mRootRef = new Firebase("https://fyp-1-43a27.firebaseio.com/User");
dbSortAllData = FirebaseDatabase.getInstance().getReference();
ListView mLvScore = (ListView) findViewById(R.id.lvScore);
final List<UserInformation> listScore = new LinkedList<>();
final ArrayAdapter<UserInformation> adapter
= new ArrayAdapter<UserInformation>
(this, R.layout.scoreboard_row, listScore) {
@NonNull
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.scoreboard_row, parent, false);
}
UserInformation item = listScore.get(position);
((TextView) view.findViewById(R.id.tvScoreName)).setText(item.getName());
((TextView) view.findViewById(R.id.tvScorePoint)).setText(item.getScore());
return view;
}
};
mLvScore.setAdapter(adapter);
com.firebase.client.Query queryRef = mRootRef.orderByChild("score").startAt("score").endAt("score").limitToFirst(50);
queryRef.addChildEventListener(new com.firebase.client.ChildEventListener() {
@Override
public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
UserInformation sI = dataSnapshot.getValue(UserInformation.class);
listScore.add(sI);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(com.firebase.client.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(com.firebase.client.DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(com.firebase.client.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
My Model Class:
public class UserInformation implements Comparable<UserInformation> {
public String name;
public String age;
public String score;
public UserInformation() {}
public UserInformation(String name, String age, String score) {
this.name = name;
this.age = age;
this.score = score;
}
// ... Getters and Setters here.
}
These piece of code works, it sorts from lowest to highest.
My Problem is how do I sort it from Highest to Lowest? Suggestions and advises are much appreciated.
Upvotes: 0
Views: 1605
Reputation: 1
if you want in reverse order then create one extra key and store that newKey = value(multiplied by)-1 than do simple order by. I have tried other solution by reversing ArrayList but when you add new data to existing list then it will get worst.
As you can see my priority score in negative so I can write
databaseReference = FirebaseDatabase.getInstance().getReference() .child("node").child("event").orderByChild("priorityScore");
Upvotes: 0
Reputation: 1367
You can reorder the data on the client side - just reverse the list.
Edit : When you want to reverse the list, you can use :
Collections.reverse(listScore);
adapter.notifyDataSetChanged();
You can also just add each element that you get from the database to the front of the list to get it in reverse order :
public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
UserInformation sI = dataSnapshot.getValue(UserInformation.class);
listScore.addFirst(sI);
adapter.notifyDataSetChanged();
}
And change the declaration of listScore to :
final LinkedList<UserInformation> listScore = new LinkedList<>();
Upvotes: 1