Convert an Object to Arraylist<Visitor>

Asked

Viewed 383 times

0

I have a method that returns values from firebase. When received, I create a new ArrayList<Visitante> and the saved in a Object.

ArrayList<Visitante> visitantes = new ArrayList<>();
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            visitantes.add(snapshot.getValue(Visitante.class));
                            Log.e("***", snapshot.getValue(Visitante.class).getNome());
                        }
event.emit(new Result(visitantes));

event.emit() wait for a guy Object.

How do I turn this Object back into the ArrayList<Visitante>?

1 answer

0


Do the cast of object for ArrayList<Visitante>.

array = (ArrayList<Visitante>) objecto; 

If the object is not of the type indicated, an exception of the type ClassCastException

  • in your if it shows an error in Arraylist<Visitor>: Illegal Generic type instanceof

  • but the cast worked! But only one question before choosing as the right answer, If the array that was assigned in Object is modified, will the new array that receives it of object be updated? that is, it passes the reference?

  • Because of type Erasure cannot be used instanceof that way. It would have to be like this: objecto instanceof ArrayList<?> and then check the type of the item. I did not put it in the answer because I think that the indicated exception is to be launched. Yes is passed to "reference".

Browser other questions tagged

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