Loop loop in Listview

Asked

Viewed 50 times

0

Can someone help me? In a listview where the data comes from an internet database I can’t let it be automatic so that all values coming are included, the only way is to add one by one, but when as is a customer list it can increase and the size will no longer be the same, then as I leave it in a loop?

The bank code is this:

<?php
header("Access-Control-Allow-Origin: *");
require_once('api.php');

if($_POST['buscar'] == "123"){
$lista = $user->getList()->fetchAll();
foreach($lista as $show){
    echo "$show->nome" . ":";

    }
}

if($_POST['buscar'] == "123"){
    echo ";";
$lista = $user->getList()->fetchAll();
foreach($lista as $show){

    echo "$show->telefone1" . ":";

    }
}

if($_POST['buscar'] == "123"){
    echo ";";

$lista = $user->getList()->fetchAll();
foreach($lista as $show){

    echo "$show->telefone2" . ":";

    }
}

if($_POST['buscar'] == "123"){
    echo ";";

$lista = $user->getList()->fetchAll();
foreach($lista as $show){

    echo "$show->endereco" . ":";

    }
}

    if($_POST['buscar'] == "123"){
    echo ";";

$lista = $user->getList()->fetchAll();
foreach($lista as $show){

    echo "$show->nota" . ":";

    }
}

    if($_POST['buscar'] == "123"){
    echo ";";

$lista = $user->getList()->fetchAll();
foreach($lista as $show){

    echo "$show->id" . ":";

    }
}



    if($_POST['buscar'] == "123"){
    echo ";";

$lista = $user->getList()->fetchAll();
foreach($lista as $show){


    $xlat2 = $show->lat;
    $xlog2 = $show->log;
    $dist = $xlat2 . "," . $xlog2.
echo intval($dist) . ":";
}

}

After the received data it transforms each item into an array:

@Override
    protected void onPostExecute(String resultado) {
        array = resultado.split(";");
        nome = array[0].split(":");
        telefone1 = array[1].split(":");
        telefone2 = array[2].split(":");
        local = array[3].split(":");
        nota = array[4].split(":");
        id2 = array[5].split(":");
        distancia = array[6].split(":");

        criarListagem();
    }

And then create the listing:

public void criarListagem() {
        ListView lista = (ListView) findViewById(R.id.lista);
        List<User> users = todosOsUsers();
        AdapterListaPersonalizado adapter = new 
        AdapterListaPersonalizado(users, this);
        lista.setAdapter(adapter);
}

public List<User> todosOsUsers() {

    return new ArrayList<>(Arrays.asList(
            new User(" Nome: " + nome[0], " Telefone 1: " + telefone1[0], " Telefone 2: " + telefone2[0], " Local: " + local[0], nota[0], " Distância≅ " + distancia[0] + " m"),
            new User(" Nome: " + nome[1], " Telefone 1: " + telefone1[1], " Telefone 2: " + telefone2[1], " Local: " + local[1], nota[1], " Distância≅ " + distancia[1] + " m"),
            new User(" Nome: " + nome[2], " Telefone 1: " + telefone1[2], " Telefone 2: " + telefone2[2], " Local: " + local[2], nota[2], " Distância≅ " + distancia[2] + " m"),
            new User(" Nome: " + nome[3], " Telefone 1: " + telefone1[3], " Telefone 2: " + telefone2[3], " Local: " + local[3], nota[3], " Distância≅ " + distancia[3] + " m"),
            new User(" Nome: " + nome[4], " Telefone 1: " + telefone1[4], " Telefone 2: " + telefone2[4], " Local: " + local[4], nota[4], " Distância≅ " + distancia[4] + " m"),
            new User(" Nome: " + nome[5], " Telefone 1: " + telefone1[5], " Telefone 2: " + telefone2[5], " Local: " + local[5], nota[5], " Distância≅ " + distancia[5] + " m"),
            new User(" Nome: " + nome[6], " Telefone 1: " + telefone1[6], " Telefone 2: " + telefone2[6], " Local: " + local[6], nota[6], " Distância≅ " + distancia[6] + " m"),
            new User(" Nome: " + nome[7], " Telefone 1: " + telefone1[7], " Telefone 2: " + telefone2[7], " Local: " + local[7], nota[7], " Distância≅ " + distancia[7] + " m"),
            new User(" Nome: " + nome[8], " Telefone 1: " + telefone1[8], " Telefone 2: " + telefone2[8], " Local: " + local[8], nota[8], " Distância≅ " + distancia[8] + " m"),
            new User(" Nome: " + nome[9], " Telefone 1: " + telefone1[9], " Telefone 2: " + telefone2[9], " Local: " + local[9], nota[9], " Distância≅ " + distancia[9] + " m"),
            new User(" Nome: " + nome[10], " Telefone 1: " + telefone1[10], " Telefone 2: " + telefone2[10], " Local: " + local[10], nota[10], " Distância≅ " + distancia[10] + " m")));

}
  • 1

    Where is the code for the data you take from the database?

  • Okay man, I updated the code, if you know how to help me I’ll be extremely grateful

1 answer

1


Whereas the vector nome has exactly the same amount of vector items telefone1, telefone2, local, nota, distancia; you can do it this way:

public List<User> todosOsUsers() {
    List<User> users = new ArrayList<>();
    for (int i = 0; i < nome.length; i++) {
        users.add(new User(" Nome: " + nome[i], " Telefone 1: " + telefone1[i], " Telefone 2: " + telefone2[i], " Local: " + local[i], nota[i], " Distância≅ " + distancia[i] + " m"));
    }
    return users;
}
  • 1

    Bro, I love you, moral, mt thank you

Browser other questions tagged

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