当前位置: 首页 > 面试题库 >

从Firebase数据库检索数据并存储在数组列表中

汪兴为
2023-03-14
问题内容

我正在尝试以android应用程序的形式创建一个小型课程聚合程序。

我的课程全部存储在Firebase实时数据库中,该数据库可从firebase控制台查看,并且一切正常。

问题是我已经编写了一个Java方法来连接到DB,从DB检索数据,将数据转换为Custom Java对象Course,将其附加到另一个Custom
Java对象CourseCardModel,然后将该CourseCardModel对象保存到ArrayList中。

与数据库的连接已成功建立,并且它生成的快照包含正确的信息,我已通过遍历快照并成功打印出Course Name每个Course对象的变量来验证这些信息。问题在于,该方法完成后,由于某种原因,即使在整个快照迭代过程中检查ArrayList的大小,我都能看到CardModel正在添加对象,但由于某种原因而返回的arrayList为空。

如果有人在解决这个问题上有任何帮助,将不胜感激,我对Firebase还是陌生的。

GenerateCourses()方法

private ArrayList<CourseCardModel> generateCourseCards() {

    courseCardModelList = new ArrayList<CourseCardModel>();
    cardModel = new CourseCardModel();

    dbref =  FirebaseDatabase.getInstance().getReference().child("courses");


    // Retrieve the course data from firebase db and cast as Course object
    dbref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            Log.e("Count " ,"" + snapshot.getChildrenCount());
            for (DataSnapshot postSnapshot: snapshot.getChildren()) {

                c = postSnapshot.getValue(Course.class);
                System.out.println("COURSE INFO: " + c.getCourseName());

                cardModel.setCourse(c);

                courseCardModelList.add(cardModel);

                System.out.println("COURSE CARD MODEL LIST SIZE: " + courseCardModelList.size());

            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("The read failed: ", databaseError.getMessage());
        }

    });

    System.out.print("END OF METHOD ARRAY SIZE CHECK: " + courseCardModelList.size());

    return courseCardModelList;

}

LOGCAT输出

01-11 09:57:57.105 28709-28780/coursematch.coursematchuk D/FA: Connected to remote service

01-11 09:57:57.105 28709-28780/coursematch.coursematchuk V/FA: Processing queued up service tasks: 4

01-11 09:57:59.625 28709-28709/coursematch.coursematchuk E/Count: 200

01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: END OF METHOD ARRAY SIZE CHECK: 0COURSE INFO: Physiotherapy BSc (Hons)

01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 1

01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: History and Archaeology BA (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 2

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Arabic and French MA (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 3

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Spanish BA (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 4

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (French and Spanish) and Greek (with year abroad) MA (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 5

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Politics BA (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 6

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Nursing (Child) BSc (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 7

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical and Electronic Engineering MEng (Hons)

01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 8

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: English and Russian MA (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 9

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and Medieval History MA (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 10

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and Modern History MA (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 11

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Physics with Sports Science BSc (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 12

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering with Management BEng (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 13

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Optometry BSc (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 14

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Sociology BA (Hons)

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 15

01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and French and Russian (with year abroad) MA (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 16

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Statistics Economics and Finance BSc (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 17

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mechanical Engineering BEng (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 18

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business Management (Spain) BBA (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 19

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Ancient History and Archaeology BA (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 20

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering MEng (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 21

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Drama and Music BA (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 22

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Chemistry BSc (Hons)

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 23

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Medicine MB

01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 24

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Linguistics MA (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 25

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Aeronautical Engineering BEng (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 26

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business and Marketing BSc (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 27

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: German and Theatre and Performance BA (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 28

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Anthropology BA (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 29

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Spanish BA (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 30

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (French and Spanish) and Greek MA (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 31

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Accounting and Mathematics and Statistics BA (Hons)

01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 32

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Art History and French MA (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 33

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Accounting and Business Analysis and Technology BA (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 34

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (Russian and Spanish) and English (with integrated year abroad) MA (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 35

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Crime BA (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 36

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business and Marketing BSc (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 37

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Statistics Economics and a Language BSc (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 38

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Work BA (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 39

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Combined Honours in Social Sciences BA (Hons)

01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 40

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and Russian and Spanish (with year abroad) MA (Hons)

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 41

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Automotive Engineering BEng (Hons)

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 42

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical Engineering MEng (Hons)

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 43

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and Russian and Spanish MA (Hons)

01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 44

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Economics and Finance (with European study) BA (Hons)

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 45

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mathematics Statistics and Finance BSc (Hons)

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 46

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and German Studies BA (Hons)

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 47

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Sociology BA (Hons)

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 48

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering with Management MEng (Hons)

01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 49

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mechanical Engineering BEng (Hons)

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 50

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Medicine (graduate entry) BMBS

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 51

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical Engineering BEng (Hons)

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 52

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Automotive Engineering MEng (Hons)

01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 53

问题答案:

调用的结果dbref.addValueEventListener()onDataChange回调中异步返回…在从该方法返回时很可能没有发生,这就是为什么courseCardModelList为空。您需要相应地构建逻辑。



 类似资料:
  • 我正在两个标记之间画一条路线,我想保存那条路线。为此,我将包含lat和lng的ArrayList保存在Firebase数据库中。但我在取回航路点时遇到了问题。我是这样插入的: 在检索数据时,我尝试执行以下操作:

  • 问题内容: 大家好,我想在html页面上显示数据库表的全部内容。我试图先从数据库中获取记录并存储在其中,但是当我在html页面上返回数组列表时,它仅重复显示最后一条记录作为数据库表的计数。这是下面的代码: 问题答案: 尝试以下代码 这是我的模特班 这是我的查看方法

  • 我想在Firebase中根据Firebase本地服务器时间和日期保存数据,而不是本地系统日期,如果用户的移动电话日期错误,则可能是错误的。然后我想通过像SQL查询一样应用日期查询(给我两个日期之间的数据并添加特定的colmn)来检索它。我如何在使用Java的安卓系统中做到这一点呢?

  • 我试图创建一个简单的程序,从用户的名字,手机号码和电子邮件地址,然后把数据在Firebase实时数据库。 有3个输入框和一个按钮,按一下就可以完成上面的操作。代码如下: 我这样设置了消防基地:

  • 我正在做一个项目,我想要一个功能,在WhatsApp上给我们留言,为此,我使用了使用intent的WhatsApp API。在“WhatsApp上向我们发送消息”功能中,我使用了2-3个不同的数字,并将其存储在列表中,并使用random()方法从列表中检索该数字,正如我所希望的那样,每当用户使用该功能时,他/她每次都必须连接不同的数字。但现在我想更改列表中存储的数字。 那么,我如何在每次不改变实际