0

I have UID inside of Top Child. Under Top Child i have 2 more child : Name and Exchange . I want to get the Top Exchange values And show them in recyclerview . The highest exchange will be in the top ,second top will be in second position and third will be in third . I am getting all values serially with firebaserecycleradapter .But i want to get the hightest value to lowest.

here is database structure : enter image description here

here is class:

TopRef = FirebaseDatabase.getInstance().getReference().child("Top");
        recyclerView = views.findViewById(R.id.topRec);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);


        Query query = TopRef.orderByChild("Exchange").limitToLast(20);
        FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<TopAdapter>()
                .setQuery(query,TopAdapter.class)
                .build();


        adapter = new FirebaseRecyclerAdapter<TopAdapter, TopFragment.TopAdapterHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull final TopFragment.TopAdapterHolder holder, int position, @NonNull TopAdapter model) {
                String userIds = getRef(position).getKey();
                assert userIds != null;
                TopRef.child(userIds).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChild("Name")){
                            String name = dataSnapshot.child("Name").getValue(String.class);
                            Long exchange = dataSnapshot.child("Exchange").getValue(Long.class);




                            holder.Name.setText(name);
                            holder.Exchange.setText(String.valueOf(exchange));




                        }


                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

            }




            @NonNull
            @Override
            public TopFragment.TopAdapterHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.topview,viewGroup,false);
                return new TopFragment.TopAdapterHolder(view);
            }
        };



        recyclerView.setAdapter(adapter);

Here is adapter class:

public class TopAdapter {
    String Name;
    String Exchange;



    public TopAdapter() {

    }


    public TopAdapter(String name, String exchange) {
        Name = name;
        Exchange = exchange;
    }

    public String getName() {
        return Name;
    }

    public String getExchange() {
        return Exchange;
    }
}

Crash Log: Found two getters or fields with conflicting case sensitivity for property: exchange

1 Answers1

1

The simplest way to order data in descending order would be on the client-side. In order to get all those objects within your Top child according to the highest value of your Exchange property, first, you need to use a query and then choose one of three options. So instead of passing TopRef to your setQuery() method, pass the following query:

Query query = TopRef.orderByChild("Exchange").limitToLast(20);

As you can see, using this query Firebase orders the results in ascending order by a given property by default. There is no method in Firebase to order in descending order but there are few tweaks that can be made.

If you are using a RecyclerView to display data, the simplest way would be to use the following code:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

This approach will reverse the order directly in your RecyclerView.

Another approach would be to create your own adapter that extends FirebaseListAdapter and override getItem() method.

Another approach would be to get the data from the database, add it to a Collection, a List would be a good solution and then use Collections.reverse(yourList);. In this case, you'll lose Firebase-UI's features.

Edit:

FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<TopAdapter>()
        .setQuery(query,TopAdapter.class) //Changed
        .build();
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Can you tell me where i should add the query ? As my code i already have a query right? –  Aug 24 '19 at 12:01
  • No, you don't have a query. Your `TopRef` is a simple reference, **not** a query. You should use a query so you can order/limit the results. There is no other way you can do that. – Alex Mamo Aug 24 '19 at 12:04
  • Can you modify my code? As if a query the value how can i show in recyclerview . I am not familiar with query and firebase recycycler adapter. –  Aug 24 '19 at 12:08
  • With what message? – Alex Mamo Aug 24 '19 at 12:26
  • Wait i am uploading the full class. –  Aug 24 '19 at 12:28
  • That's totally another issue which should new considered another question but since you're new, check [this](https://stackoverflow.com/questions/54069574/firebase-android-listview-not-being-displayed) out. – Alex Mamo Aug 24 '19 at 13:22
  • So knowing that the initial problem is solved, I will ask you now to accept my answer. I'd appreciate it. Thanks – Alex Mamo Aug 24 '19 at 13:24
  • And it also showing 0 value child which i don't want to. –  Aug 24 '19 at 15:04