0

I want to create a highscores ListView with entries being displayed in descending order, with the use of orderByValue I am getting them sorted in ascending order however when using Collections.reverse() with my arraylist, the entries are still not reversed in the ListView.

 private Query reference; //database reference
private ListView ListView; //creating listview
private ArrayList<String> arrayList = new ArrayList<>();

private ArrayAdapter<String> adapter; //creating array adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_high_scores);


    reference = FirebaseDatabase.getInstance().getReference("Highscores").orderByValue(); //reference the highscore section in database



    ListView = (ListView) findViewById(R.id.list_view); //find the list view in the xml


    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList); // set the adapter to the list
    Collections.reverse(arrayList);

    ListView.setAdapter(adapter);

    reference.addChildEventListener(new ChildEventListener() { // add a child listener for the database
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            String value = dataSnapshot.getValue().toString(); // convert the values in the databse to string
            arrayList.add(value); //add them to the arraylist
            adapter.notifyDataSetChanged(); //notify that the dataset has changed



        }

the data in the database is

{ "-M4FPEiJ_DVc9kaQrebz" : 0,

"-M4FPIuMbArT6KjtrDtO" : 6,

"-M4FPP5EdC1CaKgOVECX" : 17,

"-M4FPTWg7JkKRvWakRkH" : 12,

"-M4FPYPNVZIjhBM6gVWW" : 11,

"-M4FPbL9I-fsQwRvVIFX" : 5 }

Answer:

Managed to fix this by simply adding to the arraylist at 0 index no matter what, all other elements were simply shifted to the right leading to a listview in descending order.

            String value = dataSnapshot.getValue().toString(); // convert the values in the databse to string
            arrayList.add(0, value); //add them to the arraylist
            adapter.notifyDataSetChanged(); //notify that the dataset has changed
  • Please edit your question to show the data at `Highscores` in your database (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Apr 06 '20 at 19:50
  • Please check the duplicate to see how you can order the result descending. – Alex Mamo Apr 07 '20 at 09:57

1 Answers1

0

reference.addChildEventListener(new ChildEventListener() { // add a child listener for the database @Override public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        String value = dataSnapshot.getValue().toString(); // convert the values in the databse to string
        arrayList.add(value); //add them to the arraylist
        Collections.reverse(arrayList);  //add this line for short solution

        adapter.notifyDataSetChanged(); //notify that the dataset has changed



    }
Aadil
  • 32
  • 4
  • Tried doing this but does not seem to work, the original entries were in order (0,6,17,12,11,5). After adding your suggestion, in the ListView they are displayed in the order (17,11,5,0,6,12) – Chirag Vijay Apr 06 '20 at 16:58
  • reference = FirebaseDatabase.getInstance().getReference("Highscores").orderByValue(); Try remove orderByValue() from this reference = FirebaseDatabase.getInstance().getReference("Highscores”); – Aadil Apr 07 '20 at 09:23