1

This is my Firebase realtime database structure:

enter image description here

How to get these data in a List to pass the data to the Model Class.

When I tried by generation data like below and it works.

listCourse = new ArrayList<>();
listCourse.add(new CourseModel("UserID", "CourseCode", "CourseName"));
listCourse.add(new CourseModel("122345", "TMN1234","System Programming"));

I just want get the data from firebase in this format..

2
  • github.com/AtifAbbAsi19/Firebase-Helper/tree/master/app/src/… check Commented Apr 8, 2018 at 7:49
  • Thank you a lot for your suggession.. but is there any easy way to do it? because i already have my model class, recyclerviewadapter everything in place and working perfectly with the list i generated that i posted. i just need to get the data in this format from firebase.. i will be really grateful for any further help.. @AtifAbbAsi Commented Apr 8, 2018 at 8:05

4 Answers 4

1

You can retrieve list by this method.

  Firebase ref = new Firebase(FIREBASE_URL);

  ref.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
      Log.i("Count " ,""+snapshot.getChildrenCount());
      for (DataSnapshot postSnapshot: snapshot.getChildren()) {
        CourseModel course = postSnapshot.getValue(CourseModel.class);

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

});

Please refer this documentation: https://firebase.google.com/docs/database/android/lists-of-data

Sign up to request clarification or add additional context in comments.

3 Comments

i tried that way before but it shows 'int java.util.List.size()' on a null object reference.. when i am trying to add it in the List<CourseModel> listCourse..
can you show the code for null object reference? Have you initialized properly ? OR try this: list.add(course)
Hey! @Jay sorry i did not put the recyclerview in the correct place apparently . Its working now ... thank you for your response...
0
if (dataSnapshot.exists()) {

                int i = 1;
                for (DataSnapshot dataSnapshot1 : dataSnapshot.child(userID).child("Course").getChildren()) {

                    coursecode[i]= dataSnapshot1.getKey();
                    coursename[i]=dataSnapshot.child(userID).child("Course").child(coursecode[i]).child("CourseName").getValue(String.class);
                    listCourse.add(new CourseModel(userID,coursecode[i],coursename[i]));

                    i++;

                    }

                }

I figured it out.. this is working for me..

Comments

0

To Access Child you can to create reference.!

 DatabaseReference userRef= FirebaseDatabase.getInstance().getReference("users");
    DatabaseReference userChildRef= userRef.child(childId);
    DatabaseReference userCourseRef= userChildRef.child("courses");
    DatabaseReference userCourseIdRef= userCourseRef.child(courseId);

userCourseIdRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i(TAG, dataSnapshot.getValue(String.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});

2 Comments

ya.. that one i tried before but i need the child node and the parent node in the same List array so my one is working better for me.. rather than creating object and pass it to the array object.. Anyway thank you a lot for your suggestion..
get that list and merge it with your model
0

Tol solve this, there are to ways. First would be to get the data as objects of CourseModel class like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference coursesRef = rootRef.child("Users").child(uid).child("Courses");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<CourseModel> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            CourseModel courseModel = ds.getValue(CourseModel.class);
            list.add(courseModel);
        }
        //Do what you need to do with the list of CourseModel objects
        Log.d("TAG", list.ToString);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
coursesRef.addListenerForSingleValueEvent(valueEventListener);

The second one, would be to get the courseName as a String like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference coursesRef = rootRef.child("Users").child(uid).child("Courses");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String courseName = ds.child("CourseName").getValue(String.class);
            list.add(courseName);
        }
        //Do what you need to do with the list of strings
        Log.d("TAG", list.ToString);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
coursesRef.addListenerForSingleValueEvent(valueEventListener);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.