Display Data stored in firebase in an Android Studio activity with a list view

0

Hello I wanted to ask you a question what happens is that I have data saved in firebase in the following way users that are my different users and within these users there is a score field that contains other data

What I would like is for the score data to appear in the list view I did in my activity with its corresponding user  relice a code but every time I run it automatically closes my application I think it is a problem that does not recognize the user if someone could help me I would be grateful

I have a method to start firebase where I think the error is

private void inicializarFirebase() {
    firebaseAuth =FirebaseAuth.getInstance();

    if (firebaseAuth.getCurrentUser()== null){
        finish();
        startActivity(new Intent(this,ingreso.class));
    }

    FirebaseUser user=firebaseAuth.getCurrentUser();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    databaseReference = database.getReference("Users").child(user.getUid());




}

and this is the part of the list view

 private void listarDatos() {
    databaseReference.child("Score").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            listscore.clear();
            for (DataSnapshot objetsnatshot : dataSnapshot.getChildren()){
                Score s=objetsnatshot.getValue(Score.class);
                listscore.add(s);

                scoreArrayAdapter = new ArrayAdapter<Score>(resultados.this, android.R.layout.simple_list_item_1,listscore );
                listvscore.setAdapter(scoreArrayAdapter);
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

Thanks in advance

    
asked by Julio Tobar 20.11.2018 в 19:51
source

1 answer

0

I already solve it, I'll give you the code if someone else can help you, the problem was that I did not read the id correctly, that's why it closed and what would lead to a NullPointerException call call getUid (). I corrected this and also the Array list

 private void listaderesultado() {


    db = FirebaseDatabase.getInstance();
    auth = FirebaseAuth.getInstance();
    user = auth.getCurrentUser();
    usersRef = db.getReference("Users" ).child(auth.getUid()).child("Score");

    usersRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            ArrayList<String> listado= new ArrayList<String>();

            listado.clear();
            for (DataSnapshot objetsnatshot : dataSnapshot.getChildren()){



                Score s=objetsnatshot.getValue(Score.class);
                String cuento=s.cuento;
                String apro=s.estado_Aprovacion;
                String tiem=s.tiempo;
                listado.add("--------"+"Registro"+"-------");
                listado.add("Nivel: "+cuento);
                listado.add("Calificacion: "+apro);
                listado.add("tiempo: "+tiem);




                adapter = new ArrayAdapter<String>(resultados.this,android.R.layout.simple_list_item_1, listado);
                listvscore.setAdapter(adapter);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(resultados.this,"No salio nada",Toast.LENGTH_SHORT).show();
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });
}
    
answered by 20.11.2018 в 22:59