从Firebase检索数据在onCreateView中返回null

时间:2018-07-20 10:06:40

标签: android firebase firebase-realtime-database

成功通过onDataChange方法检索数据。但是,无法在onDataChange方法外部(在onCreateView内部)检索数据。尝试从trackList中获取数据时,它返回null。

下面是我如何从Firebase中检索数据并尝试在视图上显示。

currentUser = (User) getArguments().getSerializable("currentUserBundle");

    databaseTracks = FirebaseDatabase.getInstance().getReference("tracks").child(currentUser.getUserId());
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy");
    Date date = new Date();
    todayDate = simpleDateFormat.format(date);

    databaseTracks.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            if (trackList != null) {
                trackList.clear();
            }

            for (DataSnapshot c : children) {
                trackList.add(c.getValue(UserFoodRecord.class));
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    if (trackList != null) {
        for (UserFoodRecord u : trackList) {
            //Problem at here !!!!!!
            //return null while try to retrieve data
            dateList.add(u.getDate());
            if (u.getDate() == todayDate) {
                checkTodayRecord = true; //got today record
                UserFoodRecord tempRecord = new UserFoodRecord(u.getDate(), u.getBreakfast(), u.getLunch(), u.getDinner(), u.getOther()
                        , u.getExercise(), u.getGoal(), u.getFood(), u.getExerciseBurn());
                currentUserFoodRecord = tempRecord;
            }
        }
    }

    //return null
    if (currentUserFoodRecord != null) {
        caloriesGoal.setText(currentUserFoodRecord.getGoal().toString());
        caloriesFood.setText(currentUserFoodRecord.getFood().toString());
        caloriesExerc.setText(currentUserFoodRecord.getExerciseBurn().toString());
        caloriesRemain.setText(currentUserFoodRecord.getRemaining().toString());
    }

firebase中的数据

enter image description here

1 个答案:

答案 0 :(得分:1)

当Firebase提取数据onDataChange时,将代码移动到asynchronously

databaseTracks.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            if (trackList != null) {
                trackList.clear();
            }

            for (DataSnapshot c : children) {
                trackList.add(c.getValue(UserFoodRecord.class));
            }

            if (trackList != null) {
                for (UserFoodRecord u : trackList) {
                    dateList.add(u.getDate());
                    if (u.getDate() == todayDate) {
                        checkTodayRecord = true; //got today record
                        UserFoodRecord tempRecord = new UserFoodRecord(u.getDate(), u.getBreakfast(), u.getLunch(), u.getDinner(), u.getOther()
                                , u.getExercise(), u.getGoal(), u.getFood(), u.getExerciseBurn());
                        currentUserFoodRecord = tempRecord;
                    }
                }
            }


            if (currentUserFoodRecord != null) {
                caloriesGoal.setText(currentUserFoodRecord.getGoal().toString());
                caloriesFood.setText(currentUserFoodRecord.getFood().toString());
                caloriesExerc.setText(currentUserFoodRecord.getExerciseBurn().toString());
                caloriesRemain.setText(currentUserFoodRecord.getRemaining().toString());
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });