在Listview中检索数据

时间:2017-06-11 13:18:34

标签: android firebase firebase-realtime-database

如何从firebase数据库中检索多个数据到Listview Android。enter image description here

2 个答案:

答案 0 :(得分:0)

您需要将DatabaseReference与ChildEventListener绑定,并覆盖on ChildAdded方法。

这是一个帮助您入手的示例:

ChildEventListener mChildEventListener = new ChildEventListener() {

           @Override
           public void onChildAdded(DataSnapshot snapshot, String s) {

//Get the data that you want from the snapShot and add it to your ListView's adapter  here. Use the Adapter's Add() method. 

  } 

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

           }

           @Override
           public void onChildRemoved(DataSnapshot dataSnapshot) {

           }

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

           }

           @Override
           public void onCancelled(DatabaseError databaseError) {

           }
       };
    DatabaseReference.addChildEventListener(mChildEventListener);

You can get More Help Here.(Firebase Documentation)

答案 1 :(得分:0)

虽然您没有展示您的尝试,但下面是一个可能适合您的示例代码。首先,为您的数据库创建o模型:

public class Container {

    public HashMap<String, Model> models;
    public class Model {
        public int id;
        public String name;
    }
}

并使用它从Firebase检索数据:

FirebaseDatabase.getInstance().getReference("your path").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Container yourData = dataSnapshot.getValue(Container.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }        
}