简体   繁体   中英

Retrieve data from firebase return null in onCreateView

Success retrieve data in onDataChange method. However, cant retrieve data outside onDataChange method (inside onCreateView). While trying to get data from trackList, it return null.

Below is how i retreive data from firebase and try to display on the view.

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());
    }

Data from firebase

在此处输入图片说明

Move your code inside onDataChange as Firebase fetches data 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) {

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM