0

I have two nodes in Firebase, business and science, both under a same parent called courses

{
  "courses" : {
    "business" : {
      "-KP-EadFq_rsXymxbUas" : {
        "courseCHours" : "4",
        "courseCode" : "BIZ2000",
        "courseName" : "Business Mathematics",
        "courseYear" : "1",
        "prerequisite" : {
          "-KP-EcGRXCtlXYQvjoYo" : {
            "preMainCourse" : "Business Mathematics",
            "preSubCourse" : "null"
          }
        },
        "sections" : {
          "-KP-EbIkS1jxVfNIE5jH" : {
            "sectionCode" : "A1",
            "sectionSeats" : "20"
          },
          "-KP-EbklnKpsUIIz_U68" : {
            "sectionCode" : "A2",
            "sectionSeats" : "20"
          }
        }
      }
    },
    "science" : {
      "-KP-EKJeu5PdfyJZnIVk" : {
        "courseCHours" : "4",
        "courseCode" : "SCI3000",
        "courseName" : "Science Fundamentals",
        "courseYear" : "1",
        "prerequisite" : {
          "-KP-EOVlKZx4zofkg5RT" : {
            "preMainCourse" : "Science Fundamentals",
            "preSubCourse" : "null"
          }
        },
        "sections" : {
          "-KP-EM8vYB9d0-axX8Cr" : {
            "sectionCode" : "A1",
            "sectionSeats" : "20"
          },
          "-KP-EModVxv_Vc3SBZ_G" : {
            "sectionCode" : "A2",
            "sectionSeats" : "20"
          }
        }
      },
      "-KP-EVvdvHgqxGPsxCrE" : {
        "courseCHours" : "4",
        "courseCode" : "SCI3100",
        "courseName" : "Science History",
        "courseYear" : "1",
        "prerequisite" : {
          "-KP-EXa8naK5uF7y9py2" : {
            "preMainCourse" : "Science History",
            "preSubCourse" : "null"
          }
        },
        "sections" : {
          "-KP-EWX0hh8q5d5YTkVR" : {
            "sectionCode" : "A1",
            "sectionSeats" : "20"
          },
          "-KP-EWu8pWXS1aR9zh_4" : {
            "sectionCode" : "A2",
            "sectionSeats" : "20"
          }
        }
      }
    }
  }
}

CourseDetails model

public class CourseDetails {

    private String courseCode;
    private String courseName;
    private String courseCHours;
    private String courseYear;
    private String courseKey;

    public CourseDetails() {
    }

    public CourseDetails(String courseCode, String courseName, String courseCHours, String courseYear) {
        this.courseCode = courseCode;
        this.courseName = courseName;
        this.courseCHours = courseCHours;
        this.courseYear = courseYear;
    }

    @Exclude
    public String getCourseKey() {
        return courseKey;
    }

    public void setCourseKey(String courseKey) {
        this.courseKey = courseKey;
    }

    public String getCourseCode() {
        return courseCode;
    }

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseCHours() {
        return courseCHours;
    }

    public void setCourseCHours(String courseCHours) {
        this.courseCHours = courseCHours;
    }

    public String getCourseYear() {
        return courseYear;
    }

    public void setCourseYear(String courseYear) {
        this.courseYear = courseYear;
    }
}

Code I'm trying to get data from both nodes

ArrayList<String> businessList;
ArrayList<String> scienceList;

DatabaseReference businessRef = FirebaseDatabase.getInstance().getReference().child("courses").child("business");

businessRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        displayBusiness(dataSnapshot);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

DatabaseReference scienceRef = FirebaseDatabase.getInstance().getReference().child("courses").child("science");

scienceRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        displayScience(dataSnapshot);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

public void displayBusiness(DataSnapshot dataSnapshot) {

    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        CourseDetails c = ds.getValue(CourseDetails.class);
        String code = c.getCourseCode();
        String name = c.getCourseName();
        String CodeName = code + " " + name;

        businessList.add(CodeName);
    }
}

public void displayScience(DataSnapshot dataSnapshot) {

    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        CourseDetails c = ds.getValue(CourseDetails.class);
        String code = c.getCourseCode();
        String name = c.getCourseName();
        String CodeName = code + " " + name;

        scienceList.add(CodeName);
    }
}

Using the code above seems like returns the ArrayLists with empty data.

My goal is to retrieve the data and store in different ArrayList, businessList and scienceList. Both the ArrayLists will be used for comparing the data.

My problem is the retrieved data does not persist in the ArrayLists. How to make sure that the retrieved data is stored in the ArrayLists so that I can compare the data later on?

Lorek Bryanson
  • 195
  • 1
  • 7
  • 22

1 Answers1

1

You are not attaching the listener to the right reference.

Change it to these

businessRef.addListenerForSingleValueEvent(new ValueEventListener() {
scienceRef.addListenerForSingleValueEvent(new ValueEventListener() {

EDIT

Inside the iteration, you're not calling the getValue from the entry. Change to this

for (DataSnapshot ds : dataSnapshot.getChildren()) {
    CourseDetails c = ds.getValue(CourseDetails.class);

You want to make sure that the comparison method is called only when both of the listeners have called onDataChange, You can use my solution in another similar question or use a utility class from an answer from a firebaser here.

Community
  • 1
  • 1
Wilik
  • 7,630
  • 3
  • 29
  • 35
  • Hi again Wilik. Really sorry I forgot to update that part. Both the `Listeners` are actually correctly referenced. I updated my code – Lorek Bryanson Aug 13 '16 at 21:20
  • ah okay, can you post your database structure and `CourseDetails` class? @LorekBryanson – Wilik Aug 13 '16 at 21:25
  • Ok. Edited my question with the structure – Lorek Bryanson Aug 13 '16 at 21:36
  • For the `sections` and `prerequisite` nodes, I use another two different models – Lorek Bryanson Aug 13 '16 at 21:41
  • I'm really sorry for all the stupid mistakes. In my actual code, the `getValue()` is called correctly also. I edited my code. I'm now checking for other silly mistakes. Really sorry. Can you please check again? – Lorek Bryanson Aug 13 '16 at 21:50
  • I already tested the code. Everything can be printed out correctly if the printing is done inside `onDataChange` – Lorek Bryanson Aug 13 '16 at 21:52
  • It's exactly like my another question [here](http://stackoverflow.com/questions/38926158/android-persist-data-retrieved-from-firebase). If I wanted to compare the `ArrayLists` outside `onDataChange`, the data inside the `ArrayLists` are null – Lorek Bryanson Aug 13 '16 at 21:54
  • 1
    @LorekBryanson I see. Because these listeners work asynchronously, you can't do the comparison before both of them is finished (before `onDataChange` is called). You can use [my solution in another similar question](http://stackoverflow.com/questions/38685879/continue-execution-after-data-received-from-multiple-location-in-firebase/38686596#38686596) or use a utility class from [an answer from a firebaser here](http://stackoverflow.com/questions/38173569/only-load-layout-when-firebase-calls-are-complete/38188683#38188683). Hope those helps :) – Wilik Aug 13 '16 at 22:08
  • Thanks. Will try your solution and get back to you – Lorek Bryanson Aug 13 '16 at 22:14
  • Do you mind to go to this [chatroom](http://chat.stackoverflow.com/rooms/19132/java-and-android-era)? – Lorek Bryanson Aug 13 '16 at 22:17
  • @LorekBryanson it's better to create a new one, because it's hard to track a topic when there's a lot of people talking about multiple topics – Wilik Aug 14 '16 at 18:11
  • Hi Wilik. Thanks for replying. Can you go to this [chatroom](http://chat.stackoverflow.com/rooms/26424/iosandroidchaosoverflow)? – Lorek Bryanson Aug 14 '16 at 18:12