I have a doubt that I found nowhere, I tested part and it worked out with doubts and the other did not. I'll explain, if anyone can help.
The problem is as follows, if I have two classes called:
Class Student { String key; String nameAlumnus; String materia_Ref ... }
Class Subject { String key; String nameMaterial; List list Students; ... }
When I make a setValue with an Subject object with its populated list of students it creates in Firebase Realtime data, saves perfect. But when it comes to recovering I'm not getting it, the data does not seem to come!
Problem (Photos): It creates a list of 0 ax depending on students in the list, but when I do for example a ValueEventListener in onDataChange I try to get the data from the Subject but says it is null in the Android Studio log. / p>
I think there's got to be something with it to retrieve a list within stuff. I do not know !!!
Does anyone help?
Student
public class Aluno {
private String key;
private String nomeAluno;
private String materia_ref;
....
}
Subject
public class Materia {
private String key;
private String nomeMateria;
private List<Aluno> alunos;
....
}
Activity Saves the data:
public class TestandoObjetosListFirebaseActivity extends AppCompatActivity {
private DatabaseReference mReferenciaMateria;
private DatabaseReference mReferenciaAluno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testando_objetos_list_firebase);
mReferenciaMateria = ConfigurationFirebase.getDatabaseReference("materias");
mReferenciaAluno = ConfigurationFirebase.getDatabaseReference("alunos");
String keyMateria = mReferenciaMateria.push().getKey();
Materia materia = new Materia();
materia.setKey(keyMateria);
materia.setNomeMateria("Matemetica");
String keyAluno1 = mReferenciaAluno.push().getKey();
Aluno aluno1 = new Aluno(keyAluno1, "Pedro", keyMateria);
mReferenciaAluno.child(keyAluno1).setValue(aluno1);
String keyAluno2 = mReferenciaAluno.push().getKey();
Aluno aluno2 = new Aluno(keyAluno2, "Joaquina", keyMateria);
mReferenciaAluno.child(keyAluno2).setValue(aluno2);
String keyAluno3 = mReferenciaAluno.push().getKey();
Aluno aluno3 = new Aluno(keyAluno3, "Chalaça", keyMateria);
mReferenciaAluno.child(keyAluno3).setValue(aluno3);
String keyAluno4 = mReferenciaAluno.push().getKey();
Aluno aluno4 = new Aluno(keyAluno4, "Paizão", keyMateria);
mReferenciaAluno.child(keyAluno4).setValue(aluno4);
materia.addALunoToList(aluno1);
materia.addALunoToList(aluno2);
materia.addALunoToList(aluno3);
materia.addALunoToList(aluno4);
mReferenciaMateria.child(keyMateria).setValue(materia);
mReferenciaMateria.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Materia materia1 = dataSnapshot.getValue(Materia.class);
for(Aluno a : materia1.getAlunos()){
Log.i("app", a.getNomeAluno().toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
ActivityReadthedata:
publicclassListandoAlunosActivityextendsAppCompatActivity{privateDatabaseReferencemDBAlunos;privateButtonbtListar;privateButtonbtRemoverListener;privateListViewlvAlunosPorMateria;privateList<String>listAlunos=newArrayList<>();privateArrayAdapter<String>arrayAdapterListaAlunosPorMateria;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_listando_alunos);btListar=findViewById(R.id.onClickButton_list_alunos);btRemoverListener=findViewById(R.id.onClickEncerrar_Listner);lvAlunosPorMateria=findViewById(R.id.lv_alunos_matriculados_materia);btListar.setOnClickListener(onClickListenerListarAlunosMatriculados);btRemoverListener.setOnClickListener(onClickListenerRemoverListener);mDBAlunos=FirebaseDatabase.getInstance().getReference();}//--------------MétodoqueinstanciaoAdapteresetanoListview//MétodoserachamadonoListenerdoFirebaseprivatevoidlistaAlunosAndSetListView(){arrayAdapterListaAlunosPorMateria=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listAlunos);lvAlunosPorMateria.setAdapter(arrayAdapterListaAlunosPorMateria);}//---------------Clickquedisparaolistener------------------------privateView.OnClickListeneronClickListenerListarAlunosMatriculados=newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){mDBAlunos.child("materias").child("-L6Ts6RZ9-mlgIYTifcT").child("alunos");
mDBAlunos.addValueEventListener(listenerAlunosMatriculadosMateria);
}
};
//--------------- Click para remover Listener ValueEventListener ------------------------
private View.OnClickListener onClickListenerRemoverListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
mDBAlunos.removeEventListener(listenerAlunosMatriculadosMateria);
}
};
//--------------- Listener ValueEventListener ------------------------
private ValueEventListener listenerAlunosMatriculadosMateria = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//listAlunos.clear();
for(DataSnapshot data : dataSnapshot.getChildren()){
Aluno aluno = data.getValue(Aluno.class);
listAlunos.add(aluno.getNomeAluno().toString());
}
listaAlunosAndSetListView();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
}
FATAL EXCEPTION: main Process: com.hugo.escola, PID: 2782 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString ()' on a null object reference at com.hugo.escola.ListandoAlunosActivity $ 3.onDataChange (ListingAlunosActivity.java:86) at com.google.android.gms.internal.zzegf.zza (Unknown Source: 13) at com.google.android.gms.internal.zzeia.zzbyc (Unknown Source: 2) at com.google.android.gms.internal.zzeig.run (Unknown Source: 65) at android.os.Handler.handleCallback (Handler.java:789) at android.os.Handler.dispatchMessage (Handler.java:98) at android.os.Looper.loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6541) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)