How to pass the data from an ArrayListString to an Object?

-2

How can I pass the data I have in a ArrayList<> of type String to a custom object which then passes the data to an adapter ?.

The problem I have is that as I currently do, it generates new instances of the data, which means that every time you query the data, in RecyclerView the data is repeated every time.

Here's the code:

for (int i = 0; i < jsonArray.length(); i++) {
        //Se recorre cada posición del Array
        try {

            JSONObject hijo = jsonArray.getJSONObject(i);

            ListaPadres.add(hijo.getString("cedula_padre"));

            ListaSpinner.add(hijo.getString("nombre_hijo"));

            //Aqui es donde se instancia cada vez que llega el objeto "RecibirEstudiantes" y 
            //le paso los datos, pero quisiera saber si hay otra forma de pasarle esos mismos 
            //datos para que no se repita la información.
            adapter.addMensaje(m = new RecibirEstudiantes(ListaSpinner.get(i),ListaPadres.get(i)));

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Class "Receive Students":

public class RecibirEstudiantes {
private String nombre;
private String cedula;

public RecibirEstudiantes() {
}

public RecibirEstudiantes(String nombre, String cedula) {
    this.nombre = nombre;
    this.cedula = cedula;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getCedula() {
    return cedula;
}

public void setCedula(String cedula) {
    this.cedula = cedula;
}
}

This is what I'm looking for, something like this:

databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            try {
                //Se obtienen todas las conversaciones almacenadas en la base de datos de Firebase
                MensajeRecibir m = dataSnapshot.getValue(MensajeRecibir.class);
                //Cada mensaje que vaya recibiendo, este sera enviado al dapter
                adapter.addMensaje(m);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

    
asked by Rosyec Parrado 28.11.2018 в 18:20
source

1 answer

0

After thinking about it a bit and thanks to the help of @Orlando De La Rosa , I came up with the solution in the following way:

recyclerView.setAdapter(null);
//Le paso nulo al recyclerView
AdapterEstudiantes a;
//Creo una nueva instancia del adaptador
a = new AdapterEstudiantes(getActivity);
//Le paso el nuevo adaptador al recyclerView
reyclerView.setAdapter(a);
for (int i = 0; i < jsonArray.length(); i++) {
      a.addMensaje(m = new RecibirEstudiantes(ListaSpinner.get(i),ListaPadres.get(i)));
      //Le paso los datos al nuevo adaptador y LISTO!
    }

I could not find another way, that's why, as I said @Orlando De La Rosa , it was better to erase the previous data and go through them again.

    
answered by 28.11.2018 в 22:56