Popular Listview Shapes with Retrofit 2

Asked

Viewed 546 times

0

Although there is already a similar question, not clarified my doubt, my problem is the following, the code below is from onResponse of Retrofit, I’m starting now in Java, I would like to play the result of Response.body in listview, i managed by doing a for in stringlist.add(countryList.get(i) code. getNome()), however I would like to know if there would be a better way to do it, I saw some examples using Course, but my requests will return about 1400 lines so I do not know if using is can compromise performance, thought if there could not be another more elegant way to do, in the code below I tried to play the List with the direct Replay.body() in Arrayadapter, only that this way the app returns the data so: inserir a descrição da imagem aqui

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;
    }

}

  • The ideal would then be to work with paging. Instead of returning all results at once, make a request limiting the amount of items. Ai as you scroll, you will make a new request with another "package" of items.

  • So, the list in my case is to improve the interaction, because I think to implement a filter that shows the result in the list...but in case I can not do it, because in the case that this received data will be inserted in the sqlite, only that with the list would be better to check if the filter went well.

  • I put an answer explaining over an idea of how it could be done. = D

1 answer

2


Solving the problem of "1400 lines":

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

See below for an example passing the limit and the page as parameter. The limit represents the amount of items you want returned to each request. The 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,
    );

Therefore, you need to make a change to your webservice.php to recognize the parameters.

See below for an example of how to pass your query:

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 from the second page.

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

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

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

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

Solving of object printing:

The second problem is that you are printing an object in the list and not a single one string. To resolve you cannot enter response.body() directly to countryList. You have to wear something like:

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

And there’s another thing, as it’s a list of objects, you would have to create a Adapter customized. See here an example how create a custom list:

  • 1

    acklay I will read about then step the result, Thank you very much for the information.

  • acklay, from what I’ve seen and read, I think I’ll stick with the is, through the information you’ve given me I think it’s the simplest way to do, anything if the performance gets compromised I invest in paging, but as the app will only work on the intranet... Thank you very much!

  • 1

    @Dungacardoso the dispose! Anything give a touch here that we will try to solve. In my apps, I always put pagination because it keeps the application flowing better. When it is the case of having to download everything at once, to show in a sqlite later, create a progressibar right at the beginning to fetch all the necessary data. Therefore, the user is obliged to wait a few seconds to interact with the application. One piece of advice I give you that will help you, is to use Recyclerview and not Listview. Recyclerview is more flexible. Good luck.

  • A boy who is helping me, since I’m really crawling still recommended me to use Recyclerview, only at the moment I’m resorting to simplicity, I still have difficulties with the basics of language, but between Nullpointer, Exceptions...I’m learning kkkk

Browser other questions tagged

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