Get the value of my generated id with push in firebase

Asked

Viewed 1,316 times

0

How do I go through these ids I generated through the push and that I can get the value of this id I will need to use for the time q the user click on a list I pass this id parameter to another screen so I can load this piece of information there inserir a descrição da imagem aqui

 final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference ref = database.getReferenceFromUrl("https://appassistencia-8e7b9.firebaseio.com/empresa");

ref.addChildEventListener(new Childeventlistener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {


                final String areaAtuacao = (String) dataSnapshot.child("areaAtuacao").getValue();
                final String empresaCidade = (String) dataSnapshot.child("empresaCidade").getValue();
                final String empresaEstado = (String) dataSnapshot.child("empresaEstado").getValue();
                final String empresaImagem = (String) dataSnapshot.child("empresaImagem").getValue();
                final String empresaNome = (String) dataSnapshot.child("empresaNome").getValue();

I’m managing to catch id aq with String s, but the first time it comes null and with an id less at the end.

           System.out.println(s);
                FirebaseStorage storage = FirebaseStorage.getInstance();
                StorageReference storageRef = storage.getReferenceFromUrl("gs://appassistencia-8e7b9.appspot.com/imagens").child(empresaImagem);

                try {
                    final File localFile = File.createTempFile("images", "png");

                    storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            Log.e("Test", "success!");
                            bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
                            items.add(new Empresa(empresaNome, areaAtuacao, empresaCidade, empresaEstado, bitmap));

                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            System.out.println(s);
            }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {


        }});




recycler.setAdapter(new EmpresaAdapter(items, new EmpresaAdapter.OnItemClickListener() {

    public void onItemClick(Empresa item) {


        startActivity(new Intent(EmpresaActivity.this, Activity_Tab_Empresa.class));


    }
}));

}}

1 answer

1

A String s specifies the key of the previous widget. The first s is NULL because there is no key before the first key in the database. And following this logic, the last s will always be the penultimate key.

The key of the current element can be obtained through Datasnapshot:

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String key = dataSnapshot.getKey(); 
        }

Browser other questions tagged

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