如何从Firebase数据库获取数据?

时间:2019-07-16 09:52:11

标签: android firebase

如何从Firebase Realtimedatabase获取数据? 我在Android上很新,还没有计划如何做。有人可以建议我吗?

可以吗?

    ref.addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
    Post newPost = dataSnapshot.getValue(Post.class);
    System.out.println("Author: " + newPost.author);
    System.out.println("Title: " + newPost.title);
    System.out.println("Previous Post ID: " + prevChildKey);
  }

  @Override
  public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}

  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {}

  @Override
  public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}

  @Override
  public void onCancelled(DatabaseError databaseError) {}
});

1 个答案:

答案 0 :(得分:0)

您可以使用此:

DatabaseReference.getInstance().getReference("user").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Child child = dataSnapshot.getValue(Child.class);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
    });