如何在Firebase中使用orderBy

时间:2018-08-01 23:14:49

标签: java android firebase firebase-realtime-database

我正在使用Firebase,需要对按时间戳保存的内容进行排序。我试图制作一个orderByValue,但我不知道如何处理。  我该怎么办?

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) {

    }
});

1 个答案:

答案 0 :(得分:0)

以下代码可能会对您有所帮助。谢谢。

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());
      }
    }
    ...