0

I have the following code that splits or remove characters after a particular symbol:

rating = rating.split("\s*=\s*")[0];

I am also wondering if there is a way to add the rating numbers up for each Medical Clinic? I have a child by the name of Rating and another child called rating by username... I am trying to call the getUID, I am assuming that this will return all the users who have submitted the rating and by using the split method, I could add the rating up? Can I also show the Medical Clinic with the highest rating first? I did try to use orderbychild("Rating") but it is showing the Medical Clinics with the least rating first. I guess, I haven't added the rating yet...

DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); // getReference() is the root
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            list.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                String rating = email.split("\\s*@\\s*")[0];

                System.out.println(rating);

                int rate = (int) snapshot.child("Rating by" + rating).getValue();


             //   rating.substring(rating.indexOf("=")+1);



              //  String rating = rating.Substring(rating.IndexOf('.') + 1);

              //  rating = rating.replaceAll("[^\\d.]", "");

                Information info = snapshot.getValue(Information.class);
                assert info != null;
                String txt = "Medical Clinic" + info.getName() + "Rating" + rate;
                list.add(txt);
             //   System.out.println(rating);
                //      list.add(snapshot.getValue().toString());
            }

            adapter.notifyDataSetChanged();

        }

enter image description here

I also need to find a way to remove all characters before the = sign....

enter image description here

1 Answers1

0

To get the nodes with the highest rating, you'll want to use:

reference.orderByChild("Rating").limitToLast(10).addValueEventListener(new ValueEventListener() {

This will get you the top 10 rated nodes in ascending order.

There is no API to get them in descending order, so you will either have to reverse the results in your application code, or store an inverted value in the database to order on that. For more on this, see: Firebase Data Desc Sorting in Android

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/222853/discussion-on-answer-by-frank-van-puffelen-how-to-remove-characters-before-a-par). – Samuel Liew Oct 11 '20 at 09:21