Forms of Popular ListView with Retrofit 2

0

Although there is already a similar question, I did not clarify my question, my problem is as follows, the code below is Retrofit's onResponse, I'm starting now in Java, I'd like to play the response.body result in listView, I I got a string for the stringList.add code (countryList.get (i) .getNome ())) but I would like to know if there would be a better way to do it, I saw some examples using the Course, but my requests will return about 1400 lines so I do not know if using for can compromise the performance, I thought if there could be another more elegant way to do it, in the code below I tried to play the List with the direct response.body () in the ArrayAdapter, app returns the data like this:

public void onResponse(Call < List < Funcionarios >> call, Response < List < Funcionarios >> response) {
  try {

    List < String > stringList = new ArrayList < > ();
    ListView lista = (ListView)findViewById(R.id.ListView);
    TextView qtdtotal = (TextView) findViewById(R.id.textView);
    List < Funcionarios > countryList = response.body();

    ArrayAdapter adapter = new ArrayAdapter < > (getApplicationContext(), android.R.layout.simple_list_item_1, countryList);
     

lista.setAdapter(adapter)

} catch (Exception e) {
  Log.d("onResponse", "There is an error");
  e.printStackTrace();
}

@GET("/webservice/webservice.php")
Call < List < Funcionarios >> getFunc(@Query("empresa") String empresa);

public class Funcionarios{
    @SerializedName("NOME")
    private String nome;
    @SerializedName("MAT")
    private int matricula;
    @SerializedName("EMP")
    private int empresa;

    private List<Funcionarios> lsfunc = null;
    public List<Funcionarios> getFunc() {
        return lsfunc;
    }

    public void setFunc(List<Funcionarios> posts) {
        this.lsfunc = posts;
    }

    public int getMatricula() {return matricula ; }

    public int getEmpresa() {
        return empresa ;
    }

    public String getNome() {
        return nome;
    }

    public void setMatricula(){
        this.matricula = matricula;
    }
    public void setNome(){
        this.nome = nome;
    }
    public void setEmpresa(){
        this.empresa = empresa;
    }

}
    
asked by anonymous 30.07.2017 / 15:54

1 answer

2

Solving the "1400 rows" problem:

The ideal would be to work with pagination. Instead of returning all results at once, make a request by limiting the amount of items. As scroll goes rolling, you are making a new request with another "package" of items.

See below an example by passing the border and the page as a parameter. limit represents the number of items you want returned for each request. page represents which page you want to show.

@GET("/webservice/webservice.php)
Call <List<Funcionarios>> getFunc(
    @Query("empresa") String empresa,
    @Query("limit") int limit,
    @Query("page") int page,
    );

So you need to make a change in your webservice.php to recognize the parameters.

Here's an example of how to pass% of%:

SELECT nome, matricula, empresa FROM tblEmpresas LIMIT 0,10

This query above will return 10 items from the first page. See below 10 more items on the second page.

 SELECT nome, matricula, empresa FROM tblEmpresas LIMIT 1,10

So, you use the variables received query and $_GET['limit'] :

 SELECT nome, matricula, empresa FROM tblEmpresas LIMIT $_GET['page'].','.$_GET['limit']

This is just an example above based on what you want to do.

Solving object printing:

The second problem is that you are printing an object in the list and not a single string . To resolve you can not enter $_GET['page'] directly to response.body() . You have to use something like this:

List <Funcionarios> countryList = response.body().getFunc();

And there is something else, as is a list of objects, you would have to create a custom%%. See here an example of how create a custom list :

    
30.07.2017 / 17:08