Android How to sum total of two different list in same fragment called with retrofit2?

不打扰是莪最后的温柔 提交于 2019-12-12 05:27:36

问题


i have two lists called with retrofit 2 in the same fragment both list are of different objects and the response is working ok

Each object has a field (importe (represents money)) that i have to sum (method calculateTotal, that sum the objects of a list), so here is the problem: how can i sum the total of list1 + total of list2?? i need to show in textView the total sum of the two list:

Total.setText(totalAtenciones+totalNotas)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_rendicion, container, false);
...

...
//here is the button that executes the two requests
  imbBuscar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getAtenciones();
            getNotas();
            System.out.println("total aten:"+totalAtenciones+"  total Notas::"+totalNotas);
            calcularTotalFinal();

        }
    });
    return rootView;

}

private void getAtenciones() {
    Call<List<Atencion>> call= api.obtenerAtenciones(293,desde.getText().toString());
    call.enqueue(new Callback<List<Atencion>>() {
        @Override
        public void onResponse(Call<List<Atencion>> call, Response<List<Atencion>> response) {
            System.out.println("estamos aquiiii "+response.body());
            List<Atencion> atenciones= response.body();
            layoutManager= new LinearLayoutManager(getActivity());
            RecyclerView recycler= (RecyclerView)rootView.findViewById(R.id.rvRendicion);
            recycler.setLayoutManager(layoutManager);
            RvRendicionAdapter rvAdapter= new RvRendicionAdapter(atenciones);
            recycler.setAdapter(rvAdapter);
            calcularTotalAtenciones(atenciones);


        }

        @Override
        public void onFailure(Call<List<Atencion>> call, Throwable t) {
            System.out.println("FALLOOOOO:  "+t.getMessage());
        }
    });

}
private void getNotas() {
    Call<List<Nota>> notacall= api.obtenerNotas(293,desde.getText().toString());
    notacall.enqueue(new Callback<List<Nota>>() {
        @Override
        public void onResponse(Call<List<Nota>> call, Response<List<Nota>> response) {
            System.out.println("lista de notasssss "+response.body());
            notas= response.body();
            notasAdapter= new ListaAdaptadaNotas(getActivity(),notas);
            listaNotas.setAdapter(notasAdapter);
            calcularTotalNotas(notas);
        }

        @Override
        public void onFailure(Call<List<Nota>> call, Throwable t) {

        }
    });
}
 private void calcularTotalNotas(List<Nota> notas){
    double total=0.0;
    for (Nota nota: notas
            ) {

        if(nota.getImporte()==null){
            total=total+0.0;
        }
        else{ total= total+nota.getImporte();}
    }
    System.out.println("total notas----"+total);//here it shows the correct sum
    totalNotas=total;
}
 private void calcularTotalAtenciones(List<Atencion> atenciones){
    double total=0.0;
    for (Atencion at: atenciones
            ) {

        if(at.getImporte()==null){
            total=total+0.0;
        }
        else{ total= total+at.getImporte();}
    }
    System.out.println("total atenciones:----"+total);//here it shows the correct sum
    totalNotas=total;
}

//and this is the code that sum both totals
 private void calcularTotalFinal(){

    double total = totalAtenciones+totalNotas;//but the result is 0.0 (i lose the values)
    txtTotal.setText("Total diario = $"+total);
}

i use two variables double to save the parcial sum, totalAtenciones and totalNotas, but in the function calcularTotalFinal() both have 0.0 as value. So how can i sum the two total and show the result in textView? thanks in advance.


回答1:


I hope nota.getImporte() is a value which your getting inn your object

Change your logic like this

 for (Nota nota: notas) {

    if(nota!=null && nota.getImporte() != null){
       total= total+nota.getImporte();
         }
      }

Same to calcularTotalAtenciones also




回答2:


Because the calls are asynchronous, you are getting the values to be 0. You should compute the values after both the API calls are made.

You can have a look at RxJava2 to handle asynchronous responses elegantly



来源:https://stackoverflow.com/questions/45293911/android-how-to-sum-total-of-two-different-list-in-same-fragment-called-with-retr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!