0

Currently I have a ListView, which shows the data stored in Firebase.

I try to send the ID of each stored object, but my "putExtra" only sends the visible data, that is, the name (I have decided that this is the visible data by creating a toString method returning the name in the pojo class)

Pojo:

      private String idParcela;
        private String nombreParcela;
        private int hectareas;
        private Date ultimoTratamienetoQuimico;
        private Date proximoTratamientoQuimico;
        private String fruta;
        private String variedad;
        private String nombreTratamiento;
        private int diasTratarQuimicamente;
//GETTERS SETTERS BUILDERS...
@Override
    public String toString(){return nombreParcela; }

Now I will show the 2 functions

ListData collects the data from firebase, and stores it in the listview, with the arrayadapter.

private void listarDatos() {
    databaseReference.child("parcelas").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            listaParcelas.clear();
            for(DataSnapshot objSnaptshot : dataSnapshot.getChildren()){
                Parcela p = objSnaptshot.getValue(Parcela.class);
                listaParcelas.add(p);
            }
            arrayAdapterParcela = new ArrayAdapter<Parcela>(listaParcelas.this, android.R.layout.simple_list_item_1,listaParcelas);
            listvListaParcela.setAdapter(arrayAdapterParcela);

        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    listvListaParcela.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {

            Intent intentEditar = new Intent(listaParcelas.this,editarParcela.class);

       
 intentEditar.putExtra("idParcelaModificable",listvListaParcela.getItemAtPosition(i).toString());
            startActivity(intentEditar);
        }
    });
}

At the time of sending the intentEditar.putExtra only sends the data name and forgets all the data stored but not visible, which it contains (I know that the data is there, since in the debugger it can be observed)

  • Have you tried to add more fields inside the toString method? Besides that, have you tried to get the creation of the adapter out of the for loop? – Alex Mamo May 14 '21 at 09:59
  • I really don't want to add more information to toString, since it will show up in the layout, which I don't want. Excuse me, the second question I have not understood correctly – David Pereiras Conde May 14 '21 at 10:07
  • You are creating a new instance of your ArrayAdapter class, at every iteration of your for loop, which is not correct. You might check **[this](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)** out. I think it will help. – Alex Mamo May 14 '21 at 10:13
  • and moreover what other than `nombreParcela` you are expecting in other activity? You are sending only `nombreParcela` to other activity nothing else. – Dinkar Kumar May 14 '21 at 10:15
  • In effect, the arrayAdappter was in the wrong location. I have placed them after the for, but I still do not understand exactly how to rescue the data, checking the debug, I am not sure that they are displayed correctly, is there any way to use toString and send the id, without it being seen in the layout ? – David Pereiras Conde May 14 '21 at 12:31
  • something like that? intentEditar.putExtra("idParcelaEnviar",listvListaParcela.getItemAtPosition(i).getIdParcela()); (not working) – David Pereiras Conde May 14 '21 at 12:38

1 Answers1

0

I have finally succeeded, and I have extracted the object from the arrayadapter

After that I have extracted the id using get located in the pojo, and I have sent it to the intent.

In my case, I can send the whole object or just the id, whichever I prefer.

    listvListaParcela.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {

            Parcela DatosParcelaClick = arrayAdapterParcela.getItem(i);

            String IdParcelaString = DatosParcelaClick.getIdParcela().toString();


            Intent intentEditar = new Intent(listaParcelas.this,editarParcela.class);
            //2 options
            intentEditar.putExtra("IdParcelaClick",IdParcelaString);
            intentEditar.putExtra("objetoParcelaClick",DatosParcelaClick);
            startActivity(intentEditar);
        }
    });
}