cycle for does not travel correctly, replaces all by the last data, which are stored in a database

0

Hello Good day I'm doing my first app and what happens is that the information that will be in the application, will be extracted from a database and if I have a connection with it.

I have a cycle for with which I call several states to appear on the screen and only shows me the last data, that is not going through correctly, replaces the others by the latter.

Here is the code, your support would be of great help.

Thank you.

  @Override
        protected Void doInBackground(Void... voids) {

            rutaList = new ArrayList<>();
            conexionJson jOj = new conexionJson();
            Turismo map = new Turismo();

            try { JSONObject jsonObject = jOj.getJSONObjectFromURL("http://guanajuato.gob.mx/WSAppGto/enlace.php?metodo=pa_get_all_turismo");

                JSONArray jsonA = jsonObject.getJSONArray("Resultado");
                {
                        int id;
                        String nombre;
                        String descripcion;
                        Double latitud;
                        Double longitud;
                        String tipo;
                        String imagen;
                        int i = 0;

                    for ( i = 0; i < jsonA.length(); i++) {
                            JSONObject row = jsonA.getJSONObject(i);


                        id = row.getInt("idturismo");
                        nombre = row.getString("nombre");
                        descripcion = row.getString("descripcion");
                        latitud = row.getDouble("latitud");
                        longitud = row.getDouble("latitud");
                        tipo = row.getString("tipo");
                        imagen = row.getString("imagen");
                        switch (imagen) {
                            case "Dolores Hidalgo":
                                title = "http:\/\/doloreshidalgo.gob.mx\/images\/dolores-hidalgo-cin.png";
                                break;
                            case "Yurira":
                                title = "descarga.jpg";
                                break;
                        }


                        System.out.println(i);
                        System.out.println(nombre);



                        map.setId("1");
                        map.setNombre((String) nombre);
                        map.setDescripcion((String) descripcion);
                        map.setLatitud((Double) 0.0);
                        map.setLongitud((Double) 0.0);
                        map.setTipo((String) tipo);
                        map.setIdNombre("");
                        map.setImagen((String) imagen);
                        map.setDetalles("");
                        map.setImg(1);
                        rutaList.add(map);
                    }
                }

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


            return null;
        }

And in console if you give me the 3 names that I have in the database I / System.out: JSON:

>   {"Resultado":[{"idturismo":1,nombre:"Dolores
> Hidalgo"},{"idturismo":2,"nombre":"yuriria",{"idturismo":3,"nombre":"San
> Miguel de Allende"
>     I/System.out: 0
>     I/System.out: Dolores Hidalgo
>     I/System.out: 1
>     I/System.out: yuriria
>     I/System.out: 2
>     I/System.out: San Miguel de Allende
    
asked by Angie.Edu 03.03.2017 в 20:52
source

2 answers

0

You can try the following:

    rutaList = new ArrayList<>();
    conexionJson jOj = new conexionJson();
    Turismo map = new Turismo();

    try { JSONObject jsonObject = jOj.getJSONObjectFromURL("http://guanajuato.gob.mx/WSAppGto/enlace.php?metodo=pa_get_all_turismo");

        JSONArray jsonA = jsonObject.getJSONArray("Resultado");
        {
                int id;
                String nombre;
                String descripcion;
                Double latitud;
                Double longitud;
                String tipo;
                String imagen;
                int i = 0;

            for ( i = 0; i < jsonA.length(); i++) {
                    JSONObject row = jsonA.getJSONObject(i);


                id = row.getInt("idturismo");
                nombre = row.getString("nombre");
                descripcion = row.getString("descripcion");
                latitud = row.getDouble("latitud");
                longitud = row.getDouble("latitud");
                tipo = row.getString("tipo");
                imagen = row.getString("imagen");
                switch (imagen) {
                    case "Dolores Hidalgo":
                        title = "http:\/\/doloreshidalgo.gob.mx\/images\/dolores-hidalgo-cin.png";
                        break;
                    case "Yurira":
                        title = "descarga.jpg";
                        break;
                }


                System.out.println(i);
                System.out.println(nombre);



                map.setId("1");
                map.setNombre((String) nombre);
                map.setDescripcion((String) descripcion);
                map.setLatitud((Double) 0.0);
                map.setLongitud((Double) 0.0);
                map.setTipo((String) tipo);
                map.setIdNombre("");
                map.setImagen((String) imagen);
                map.setDetalles("");
                map.setImg(1);
                //setea la posición del elemento a agregar
                rutaList.add(i, map);
            }
        }

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


    return null;
}
    
answered by 03.03.2017 в 21:05
0

I think the problem is that you are adding rutaList the same instance x times, you modify it in each iteration, with the use of setters ect, but the instance is the same, you do not notice it at the beginning, or I would not notice it even if I made System.out.println(map.setNombre()); within for because it would show the last value that instance has for that field, in this case for the name.

Now that is reflected when you go through the ArrayList at some point, because although the capacity of ArrayList is 3, all are from the same instance.

An example I hope will help you:

This one works in a similar way to what you say:

ideone test 1

And this works as I think you would expect:

ideone test 2

Notice that the main change is that within for a new instance is created for Test , which in this case is the simulation of your Tourism class, which is then passed to the ArrayList.

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

        ArrayList<Test> rutaList = new ArrayList<>();
        //Test map = new Test();
        String[] testa = {"hola", "test", "aqui"};

        String nombre;


        for ( int i = 0; i < 3; i++) {

            Test map = new Test(); 

            nombre = testa[i];

            System.out.println(i);
            System.out.println(nombre);

            map.setNombre((String) nombre);

            rutaList.add(map);
        }


        //miramos rutaList
        System.out.println("---");

        for(int a = 0; a < rutaList.size(); a++){
            System.out.println(rutaList.get(a).getNombre());
        }
    }
}

class Test{

    String nombre;

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

    public String getNombre(){
        return this.nombre;
    }
}

I've tried to explain it in a simple way I hope you understand.

PS: I have a for cycle with which I call several states to appear on the screen and it only shows me the last data, so it is not traveling correctly ... Possibly if it is traveling correctly, it is simply possible that the error is in what is exposed in the answer.

    
answered by 04.03.2017 в 16:32