【问题标题】:How can I pull data from realtime firebase database into an array? I wish to change static data in an array to firebase data如何将实时 Firebase 数据库中的数据提取到数组中?我希望将数组中的静态数据更改为 firebase 数据
【发布时间】:2019-01-23 17:22:02
【问题描述】:

我创建了一个自定义日历,它使用从 github 链接 https://github.com/DeveloperBrothers/Custom-Calendar-View 获取的数组“HomeCollection”,以便在我的 android 应用程序中创建自定义日历。目前,事件数据被硬编码到这个数组中(参见下面的代码 sn-p),但希望从我的 firebase 数据库中获取事件属性,而不是在我创建数据库并保存此类事件数据的地方(参见下面的图片链接)。

HomeCollection.date_collection_arr = new ArrayList<HomeCollection>();
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-21", "Winter Table Quiz", "7-10 pm, Clubhouse", "ClubHouse", "Come along to the Winter Table Quiz, €5pp, tables of 4, theres lots of great prizes to be won."));
       HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-03", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-03", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-10", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-10", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-17", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-17", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-24", "u21 Division 1 Training ", "10-11am, Club Grounds", "ClubHouse", "Weekly Division 1 training. Weather dependant."));
        HomeCollection.date_collection_arr.add(new HomeCollection("2018-11-24", "u21 Division 2 Training ", "11-12am, Club Grounds", "ClubHouse", "Weekly Division 2 training. Weather dependant."));*/
        cal_month = (GregorianCalendar) GregorianCalendar.getInstance();
        cal_month_copy = (GregorianCalendar) cal_month.clone();
        hwAdapter = new HwAdapter(this, cal_month, HomeCollection.date_collection_arr);

谁能帮我将此静态代码更改为我的 Firebase 数据库数据?

Link to my firebase JSON events tree image

【问题讨论】:

  • 您希望得到什么预期结果?

标签: java android arrays firebase android-database


【解决方案1】:

因此,您可以制作一个 Java bean 来保存所有这些数据。为您作为对象存储的所有数据创建gettersetter 方法。例如,为对象属性创建 gettersetter 方法,例如:eventDate、eventDescription 等,并假设您将 bean 命名为 EventBean.java

现在像这样使用childEventListener

mDatabaseReference = mFirebaseDatabase.getReference().getChild("event");
mDatabaseReference.addChildEventListener(new OnChildEventListener(){

@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName){
EventBean data = snapshot.getValue(EventBean.class);//now you have got data in the 
eventbean instance
System.out.println(data.getEventDate());
}
});

我希望这就是你要找的。​​p>

【讨论】:

    【解决方案2】:

    1) 您必须在代码中获取 firebase 数据引用

    DatabaseReference mFireBaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
    

    2) 然后你添加了 firebase 监听器来获取数据:

    mFireBaseDatabaseReference.child("events").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    
                    //you can parse the data in your models here like this
    for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
    Event event = childSnapshot.getValue(Event.class);
    HomeCollection.date_collection_arr.add(event);
    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });
    

    【讨论】:

    • 您可以关注firebase android文档了解更多关于firebase realtimedatabase的信息:firebase.google.com/docs/database/android/lists-of-data
    • 我在使用上述代码时收到一个错误,但它让我想到了以下代码:
    • mFireBaseDatabaseReference.child("events").addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { 事件事件= childSnapshot.getValue(Event.class); HomeCollection.date_collection_arr.add(new HomeCollection(event.getEventDate(),event.getEventName(),event.getEventTime(),event.getEventLocation(),event.getEventDescription())) ; }
    • 但 event.getTime() 等似乎没有将数据从 firebase 拉到数组中
    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 2014-04-11
    • 2019-02-26
    • 2022-07-08
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多