Display data in a ListView from a database

0

I am decoding the response in JSON , and I want to show that data in ListView , how can I do this task? . I leave my progress.

public void onResponse(String response) {

                    System.out.println("INFORMACIÓN OBTENIDA DE LA BASE DE DATOS:");
                    //Crear un Objeto JSON a partir del string response
                    Object jsonObject = JSONValue.parse(response.toString());
                    //Convertir el objeto JSON en un array
                    JSONArray array = (JSONArray) jsonObject;

                    ArrayList<Usuarios> list = new ArrayList();

                    List<String> al = new ArrayList<String>();

                    //Iterar el array y extraer la información
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject row = (JSONObject) array.get(i);
                        String nombre = row.get("usuario").toString();
                        String conexion= row.get("conexion").toString();
                        System.out.println("Usuario: " + nombre + " || 
                        Conexion: " + conexion);

                        al.add(nombre);
                        al.add(conexion);
                    }
                    ListView d = (ListView) findViewById(R.id.listView);
                }
    
asked by Santiago Jorda 16.01.2017 в 17:19
source

2 answers

2

Having your data in ArrayList :

 List<String> al = new ArrayList<String>();

and having the ListView instance, you can fill your ListView first by creating an Adapter with the data obtained:

//Crea Adapter.
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, al);  

and then configure this Adapter to your ListView,

  //Configura Adapter al ListView.  
  d.setAdapter(listAdapter);  

I add code:

   ...
   ...
  //Iterar el array y extraer la información
                for (int i = 0; i < array.size(); i++) {
                    JSONObject row = (JSONObject) array.get(i);
                    String nombre = row.get("usuario").toString();
                    String conexion= row.get("conexion").toString();
                    System.out.println("Usuario: " + nombre + " || 
                    Conexion: " + conexion);

                    al.add(nombre);
                    al.add(conexion);
                }
                ListView d = (ListView) findViewById(R.id.listView);

              //Crea Adapter con datos obtenidos.
              ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, al);  

              //Configura Adapter al ListView.  
               d.setAdapter(listAdapter);   

I recommend that instead of having nombre and conexion in different cell you can add the 2 in it:

//al.add(nombre);
//al.add(conexion);
al.add(nombre + " " + conexion);

I add an article that contains an example Example of Android ListView

    
answered by 16.01.2017 / 18:14
source
1

With list the list you want to appear in ListView :

.
.
.
ListView d = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> itemsAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list.toArray(new String[0]));
d.setAdapter(itemsAdapter);
    
answered by 16.01.2017 в 17:22