0

How come it doesn't show every structure, but only the first of the repeated list? What am I doing wrong?

I want to sort by desc rating

Here is the structure of the data in Firebase,

    Structure
         |
         id_structure
                |
                rating: 7
final FirebaseRecyclerAdapter<Structure,StructureViewHolder> adapter=
    new FirebaseRecyclerAdapter<Structure, StructureViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull StructureViewHolder structureViewHolder, int i, @NonNull Structure structure) {
            StructureRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    List<Structure> structureList=new ArrayList<>();
                    for(DataSnapshot d:dataSnapshot.getChildren()){
                        Structure structure=d.getValue(Structure.class);
                        structureList.add(structure);
                    }
                    Collections.reverse(structureList);
                    for(Structure s:structureList) {
                        structureViewHolder.structureRating.setText(String.format(Locale.ITALY, "%.1f", s.getRating()));                                   

                    }
                }                                                                                                               
};
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
HalNap
  • 39
  • 1
  • 1
  • 5
  • Firebase always returns results in ascending order. You'll have to either reverse them on the client, or add a property with an inverted value to your data and sort on that. See many of these [search results](https://stackoverflow.com/search?q=%5Bfirebase-realtime-database%5D%5Bandroid%5D+descending). – Frank van Puffelen Feb 08 '20 at 17:12
  • Hi thanks I have already seen. My problem is in the second For, because it shows only one repeated element for structurelist.size (). I do not understand why – HalNap Feb 08 '20 at 18:01
  • We're missing some information that is crucial to be able to help here. If this is about the `StructureRef.addValueEventListener` please only show *that* code, and the the JSON at `StructureRef` (as text, no screenshot 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). Also see [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Frank van Puffelen Feb 08 '20 at 18:46

0 Answers0