I cannot recover a list of Firebase objects

Asked

Viewed 525 times

0

I have a doubt that I did not find anywhere, I tested part and it worked with doubts and the other did not give. I will explain, if someone can help.

The problem is this, if I have two classes called:

Class Aluno{ String key; String name String materia_Ref ... }

Class Materia{ String key; String nameMaterial; List listAlunos; ... }

When I make a setValue with a Materia object with its populated student list it creates in Firebase Realtime the data, saves perfect. But at the time of recovering I’m not getting, the data doesn’t seem to come!!!

Problem (Photos) :: It creates a list from 0 to x depending on students in the list, but when I do for example a Valueeventlistener on onDataChange I try to get the data from Materia but says it is null in the Android Studio log.

I think it has something to do with him recovering a list within matter. I don’t know!!!

Anyone can help?

Pupil

public class Aluno {

    private String key;
    private String nomeAluno;
    private String materia_ref;
....
}

Materia

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) {

            }
        });
    }
}

inserir a descrição da imagem aqui

Activity Le the data:

    public class ListandoAlunosActivity extends AppCompatActivity {

    private DatabaseReference mDBAlunos;
    private Button btListar;
    private Button btRemoverListener;
    private ListView lvAlunosPorMateria;

    private List<String> listAlunos = new ArrayList<>();

    private ArrayAdapter<String> arrayAdapterListaAlunosPorMateria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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étodo que instancia o Adapter e seta no Listview
    // Método sera chamado no Listener do Firebase
    private void listaAlunosAndSetListView(){
        arrayAdapterListaAlunosPorMateria = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    listAlunos);
        lvAlunosPorMateria.setAdapter(arrayAdapterListaAlunosPorMateria);

    }


    //--------------- Click que dispara o listener ------------------------
    private View.OnClickListener onClickListenerListarAlunosMatriculados = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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(Listandoalunosactivity.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)

1 answer

1

I believe the error is here in this part

private List<String> listAlunos = new ArrayList<>();

you are initiating the list outside the creation method, try to put it inside the onCreate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.