我使用ChildEventListener按照文档进行操作。在数据加载期间,我仅仅在ListView中显示一个进度动画。
- If the list is not empty, then I hide/null the progress view.
If the list is void, I display "no item"
// Get a reference to our posts Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); ref.addChildEventListener(new ChildEventListener() { // Retrieve new posts as they are added to the database @Override public void onChildAdded(DataSnapshot snapshot, String previousChildKey) { BlogPost newPost = snapshot.getValue(BlogPost.class); System.out.println("Author: " + newPost.getAuthor()); System.out.println("Title: " + newPost.getTitle()); } //... ChildEventListener also defines onChildChanged, onChildRemoved, // onChildMoved and onCanceled, covered in later sections. });
使用ref.addValueEventListener
,我们只需检查datasnapshot
是否为空即可知道它的情况。
如何知道列表为空?这可能吗?不用先使用addValueEventListener
进行检查吗?
DataSnapshot.hasChildren()
吗?文档表示 "指示此快照是否具有任何子项"。 - OneCricketeer