Android如何将使用retrofit2调用的同一片段中的两个不同列表的总和? [英] Android How to sum total of two different list in same fragment called with retrofit2?

查看:47
本文介绍了Android如何将使用retrofit2调用的同一片段中的两个不同列表的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一片段中有两个以翻新2形式调用的列表,两个列表都属于不同的对象,并且响应正常

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

每个对象都有一个我必须求和的字段(import(代表钱))(methodTotal,求和列表对象的总和),所以这是问题所在:我如何求和list1 + total的总和list2 ??我需要在textView中显示两个列表的总和:

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)

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

我使用两个变量double来保存总计值,totalAtenciones和totalNotas,但是在函数calcularTotalFinal()中,它们的值均为0.0.那么我如何将两个总数相加并在textView中显示结果呢?提前致谢.

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.

推荐答案

由于调用是异步的,因此将值设为0.在两次API调用完成后,应计算值.

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.

您可以查看 RxJava2 ,以优雅地处理异步响应

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

这篇关于Android如何将使用retrofit2调用的同一片段中的两个不同列表的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆