title: Firebase - retrieve data in Android
body: <p>I have a data looks like this:</p>
  
  <pre><code>{
   "courses" : {
      "Business" : {
        "-KOBuojCGl_KVNgCx6l3" : {
          "courseCode" : "BUS2202",
          "courseName" : "Business Mathematics"
        },
        "-KOCH9RvYkwIamb0oxi8" : {
          "courseCode" : "BUS2302",
          "courseName" : "Business Fundamentals"
        }
      },
      "Computing &amp; IT" : {
        "-KOBukFDN8eDjzF77haS" : {
          "courseCode" : "SDT2201",
          "courseName" : "System Designs"
        },
        "-KOBuriS_C5njfhAkV_P" : {
          "courseCode" : "SDT3201",
          "courseName" : "Computer Project"
        },
        "-KOD-E81rtXhzEmSeRPx" : {
          "courseCode" : "SDT2202",
          "courseName" : "System Designs Year 2"
        },
      }
    }
  }
  </code></pre>
  
  <p>And an UI looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/3KpWo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3KpWo.jpg" alt="enter image description here"></a></p>
  
  <p>When user selects Business Fundamentals, <code>textview1</code> should display BUS2302 and <code>textview2</code> should display Business Fundamentals.</p>
  
  <p>Codes I'm trying now:</p>
  
  <pre><code>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) {
  
              }
          });
      }
  }
  </code></pre>
  
  <p><code>CourseDetails</code> class:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>When I run the codes, both the textviews just blank, and no error message. When I changed <code>mRef</code> ValueEventListener to this:</p>
  
  <pre><code>mRef.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  tv1.setText(dataSnapshot.getValue().toString());
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
  
              }
          });
  </code></pre>
  
  <p>The whole JSON data printed out in <code>textview1</code>, so I know my data is there. But when I use <code>getCourseCode()</code> and <code>getCourseName()</code>, the textviews are blank. Any suggestions?</p>
  
  <p>I have another question which the codes will be used to get the <code>push key</code> of a course, then only display the course code and name accordingly. The question can be found <a href="https://stackoverflow.com/questions/38808970/android-get-push-key-based-on-a-child-firebase/38809229#38809229" title="here">here</a></p>