0

I'm working with firebase and I need to sort things saved by timestamp. I tried to make an orderByValue, but I do not know how to deal with it. How can I do this?

enter image description here

String emailUsuario = autenticacao.getCurrentUser().getEmail();
String idUsuario = CustomBase64.codificarBase64( emailUsuario );
historicoRef = firebaseRef.child("historico").child( idUsuario );

valueEventListenerFavoritos = historicoRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        capitulos.clear();
        if(dataSnapshot.getValue() != null) {
            for (DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
                for (DataSnapshot dados : dSnapshot.getChildren()) {
                    final Capitulos capitulos2 = dados.getValue(Capitulos.class);
                    capitulos.add(capitulos2);
                    //progressBar.setVisibility(View.GONE);
                }
            }
        } else {
            //textFavoritos.setText("Favorite um mangá");
            //progressBar.setVisibility(View.GONE);
        }
        adapterHistorico.notifyDataSetChanged();
    }



    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
OliverDamon
  • 147
  • 9
  • [The search feature is your friend](https://stackoverflow.com/questions/41976747/android-order-firebase-array-by-timestamp) – El1f Aug 02 '18 at 00:11
  • Even if firebase didn't support ordering, you can do `Collections.sort(capitulos, new Comparator() ... )` – OneCricketeer Aug 02 '18 at 01:09
  • 1
    There seem to be two levels between `/historico/$idUsuario` and the value you want to order/filter on. Firebase Database queries can only order/filter on properties that are one dynamic level deep. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Aug 02 '18 at 03:38

1 Answers1

0

The below code may help you. thanks.

historicoRef = firebaseRef.child("historico");
historicoRef.orderByChild("idUsuario ").equalTo("some id here")
  .addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
      for (DataSnapshot child: snapshot.getChildren()) {
        System.out.println(child.getKey());
      }
    }
    ...
Gourango Sutradhar
  • 1,461
  • 10
  • 16