Reputation:
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 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
Upvotes: 0
Views: 194
Reputation: 138824
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();
Upvotes: 1