I want to retrieve the structure as follows:
Courses>Course>Programme>Sem>{SubjectName}>total>attendance>orderbykey.equalsTo(uid)
I have a issue iterating through the subjects where the key is dynamic, like for for eg. here the key for one is PA and other is PC and it will be dynamically pushed by the user and will be different for every Sem.
I have tried this:
Query subjectQuery = mDatabaseRef.child("courses/"+user.getCourse()+"/"+user.getProgramme()+"/"+user.getSem()+"/subjects");
subjectQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Log.d("subjects", String.valueOf(dataSnapshot.getChildrenCount()));
sub = new Subject[(int) dataSnapshot.getChildrenCount()];
int i = 0;
for (DataSnapshot subjectSnapShot: dataSnapshot.getChildren()){
sub[i] = new Subject(subjectSnapShot.getValue().toString());
Log.d("subject_class","sub["+i+"] : "+sub[i].getName());
i++;
}
}else{
Toast.makeText(DashboardActivity.this, "Data Not Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Toast.makeText(this, String.valueOf(sub.length), Toast.LENGTH_SHORT).show();
As The Listener is Async
the toast makes the app crash and gives a NullPointerException
as the sub
is still null
edited: the reason for this is to get the subjects and pass it to the fragment. please suggest if anyone has a better way to go around the problem