how Firebase – retrieve data in Android

 

Questions


I have a data looks like this:

{
 "courses" : {
    "Business" : {
      "-KOBuojCGl_KVNgCx6l3" : {
        "courseCode" : "BUS2202",
        "courseName" : "Business Mathematics"
      },
      "-KOCH9RvYkwIamb0oxi8" : {
        "courseCode" : "BUS2302",
        "courseName" : "Business Fundamentals"
      }
    },
    "Computing & IT" : {
      "-KOBukFDN8eDjzF77haS" : {
        "courseCode" : "SDT2201",
        "courseName" : "System Designs"
      },
      "-KOBuriS_C5njfhAkV_P" : {
        "courseCode" : "SDT3201",
        "courseName" : "Computer Project"
      },
      "-KOD-E81rtXhzEmSeRPx" : {
        "courseCode" : "SDT2202",
        "courseName" : "System Designs Year 2"
      },
    }
  }
}

And an UI looks like this:

enter image description here

When user selects Business Fundamentals, textview1 should display BUS2302 and textview2 should display Business Fundamentals.

Codes I’m trying now:

public class MainActivity extends AppCompatActivity{

    private TextView tv1, tv2;
    private DatabaseReference mRef;
    private String code, name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRef = FirebaseDatabase.getInstance().getReference().child("courses").child("Business");
        tv1 = (EditText) findViewById(R.id.textview1);
        tv2 = (EditText) findViewById(R.id.textview2);

        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                CourseDetails courseDetails = dataSnapshot.getValue(CourseDetails.class);
                code = courseDetails.getCourseCode();
                name = courseDetails.getCourseName();
                tv1.setText(code);
                tv2.setText(name);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

CourseDetails class:

public class CourseDetails {
    private String courseCode;
    private String courseName;

    public CourseDetails() {
    }

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

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

When I run the codes, both the textviews just blank, and no error message. When I changed mRef ValueEventListener to this:

mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                tv1.setText(dataSnapshot.getValue().toString());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

The whole JSON data printed out in textview1, so I know my data is there. But when I use getCourseCode() and getCourseName(), the textviews are blank. Any suggestions?

I have another question which the codes will be used to get the push key of a course, then only display the course code and name accordingly. The question can be found here

 

 

————————————————-

Answer

In your MainActivity for mRef it reference till node Business of the database which has further two children with keys which I think are generated using push. So, I think you have to iterate over it’s children. Can you try something like below:

 mRef.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
   // for each children
    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

        CourseDetails courseDetails = snapshot.getValue(CourseDetails.class);
        code = courseDetails.getCourseCode();
        name = courseDetails.getCourseName();
        tv1.setText(code);
        tv2.setText(name);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

}

If this gives some result apart from null, then may have to work in mapping textViews with respective to key.

android,event-listener,firebase,firebase-database

Facebook Comments

Post a comment