【问题标题】:I actually want to retrieve data from the firebase database in order of recent one first [duplicate]我实际上想按最近的顺序从firebase数据库中检索数据[重复]
【发布时间】:2018-12-28 11:49:34
【问题描述】:

这是我的数据库参考:

firebaseDatabase = FirebaseDatabase.getInstance();
    myRef = (DatabaseReference) firebaseDatabase.getReference().child("Notifications").orderByChild("Date");

这是我的 Firebase 控制台的快照:

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    这是我从 Firebase 检索数据的方法:

    1) 制作帮助方法以附加和分离您的 Listener。每当您的实时数据库中发生更改时,都会调用此代码。

    // Firebase instance variables
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mNotificationsDatabaseReference;
    private ChildEventListener mChildEventListener;
    
    private void attachDatabaseReadListener() {
            if (mChildEventListener == null) {
                mChildEventListener = new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    
                        Notification currentNotification= dataSnapshot.getValue(Notification.class); //deserialize data (make it an object again)
                        mNotificationAdapter.add(currentNotification);
    
                        //Store your data here in what order you like.
                    }
    
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                    }
    
                    public void onChildRemoved(DataSnapshot dataSnapshot) {
                    }
    
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                    }
    
                    public void onCancelled(DatabaseError databaseError) {
                    }
                };
                mNotificationsDatabaseReference.addChildEventListener(mChildEventListener);
            }
        }
    
    
    
    private void detachDatabaseReadListener() {
        if (mChildEventListener != null) {
            mNotificationsDatabaseReference.removeEventListener(mChildEventListener);
            mChildEventListener = null;
        }
    }
    

    2)不要忘记附加和分离监听器 onResume 和 onPause。

    @Override
    protected void onResume() {
        super.onResume();
        attachDatabaseReadListener();  
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mNotificationAdapter.clear(); //Delete data in adapter which populates a listview. Otherwise you will end up with double items.
        detachDatabaseReadListener();
    }
    

    如需更多信息和帮助,请阅读official documentation

    如果您真的想了解 Firebase 的基础知识,我建议您查看这个惊人的 tutorial(由 Google 制作)。

    【讨论】:

      【解决方案2】:

      您可以按如下方式检索最近的数据:

      List<Notifications> notificationList = new ArrayList();
      firebaseDatabase = FirebaseDatabase.getInstance();  
      myRef = (DatabaseReference) firebaseDatabase.getReference().child("Notifications").orderByKey();
       myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                  @Override
                  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                      for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                              Notifications notification = snapshot.getValue(Notifications.class);
                              notificationList.add(0,notification);
                      }
      
                  }
      
                  @Override
                  public void onCancelled(@NonNull DatabaseError databaseError) {
      
                  }
              });
      

      使用 firebase,您应该在客户端执行一些查询逻辑。 Firebase 不支持降序和升序。

      【讨论】:

      • 查询声明在哪里?
      • @abhishekdas 这是我的错误,我已经更新了我的答案,请检查
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多