如何将数据从实时Firebase数据库提取到阵列中?我希望将数组中的静态数据更改为Firebase数据

时间:2019-01-23 17:22:02

标签: java android arrays firebase android-database

我创建了一个自定义日历,该日历使用从github链接https://github.com/DeveloperBrothers/Custom-Calendar-View中获取的数组“ HomeCollection”,以便在我的android应用程序中创建一个自定义日历。当前,事件数据已硬编码到此数组中(请参见下面的代码片段),但希望从我的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

2 个答案:

答案 0 :(得分:0)

因此,您可以制作一个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());
}
});

我希望这是您想要的。

答案 1 :(得分:0)

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) {

            }
        });