【问题标题】:Method does not execute when called. FIRESTORE调用时方法不执行。火灾商店
【发布时间】:2018-01-08 05:34:56
【问题描述】:

我有一个方法: calcularPrecio() ,当我单击按钮时会调用它。该方法必须更新价格 (precio) 并在 TextView 中设置文本。之后,我从同一个 TextView 的方法外部检索文本,然后将其分批上传到 Firestore。

问题是价格是在方法完成之前在批次中设置的,所以我在批次中获取旧价格,然后Textiew更新。

我用调试器查看了它。它开始执行方法,然后出去,然后返回完成方法,我不知道为什么!

这是按钮的代码:

    mRealizarPedido = findViewById(R.id.pedido_boton_realizarPedido);
    mRealizarPedido.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         extra.put("timestamp", FieldValue.serverTimestamp());
                extra.put("aprobado", false);
                calcularPrecio(editTextArray,nombres,posiciones);
                extra.put("precio", Integer.parseInt(mTextoPrecio.getText().toString()));
                Log.d("outside:  ", String.valueOf(extra.get("precio")));
                Barriles.put("Barriles",barriles);
                Botellas.put("Botellas",botellas);
                Monjitas.put("Monjitas",monjitas);                   
                mLote.set(refPedido , Barriles, SetOptions.merge());
                mLote.set(refPedido , Botellas, SetOptions.merge());
                mLote.set(refPedido , Monjitas, SetOptions.merge());

                //Cargo al lote informacion extra
                mLote.set(refPedido, extra, SetOptions.merge());



                //Subo el lote
                mPedidoProgress.setVisibility(View.VISIBLE);
                mLote.commit().addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {

                        Toast.makeText(PedidoActivity.this, "Pedido Agregado",
                                Toast.LENGTH_LONG).show();

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                        Toast.makeText(PedidoActivity.this, "Error agregando el pedido al servidor",
                                Toast.LENGTH_LONG).show();


                    }
                });

                // Bunch of code that is not relevant...


            }

        }
    }); 

这就是方法:

private void calcularPrecio(final EditText[] editTextArray, final String[] nombres, final List<Integer> posiciones) {

    mBaseDatos.collection("Precios").document("precios").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {

            if (task.isSuccessful()) {

                DocumentSnapshot doc = task.getResult();

                if (doc != null) {

                    int precio = 0;
                    Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());

                    for(int j=0; j<posiciones.size(); j++){

                        int i = posiciones.get(j);
                        int cantidad = Integer.parseInt(editTextArray[i].getText().toString());
                        String nombre = nombres[i];

                        if (i <= 8){

                            int p = (int) Integer.valueOf(String.valueOf(doc.get("Barriles." + nombre)));
                            precio += cantidad * p;

                        } else if (8 < i && i <= 17){

                            int p = (int) Integer.valueOf(String.valueOf(doc.get("Botella." + nombre)));

                            precio += cantidad * p;

                        }else{

                            int p = (int) Integer.valueOf(String.valueOf(doc.get("Monjitas." + nombre)));
                            precio += cantidad * p;

                        }

                    }

                    mTextoPrecio.setText(String.valueOf(precio));

                    Log.d("inside:  ", String.valueOf(extra.get("precio")));

                } else {

                    Log.d(TAG, "No existe el documento");

                }

            } else {

                Log.d(TAG, "get fallo con la exepción: ", task.getException());

            }

        }
    });

}

如果我运行代码,在 Logcat 中我首先会得到以下输出:

Log.d("outside:  ", String.valueOf(extra.get("precio")));

然后输出:

Log.d("inside:  ", String.valueOf(extra.get("precio")));

应该是相反的!我错过了什么?如何获取更新的价格以上传到批次?

谢谢!

【问题讨论】:

    标签: java android google-cloud-firestore


    【解决方案1】:

    您必须在执行OnCompleteListeneronComplete 方法后调用方法,因此创建一个名为updateDetails() 的新方法并在Log.d("inside: " 之后调用它

    public void updateDetails()
        {
         extra.put("precio", Integer.parseInt(mTextoPrecio.getText().toString()));
    
                    Log.d("outside:  ", String.valueOf(extra.get("precio")));
    
    
                    //Cargo las cervezas a los Mapa de datos generales
                    Barriles.put("Barriles",barriles);
                    Botellas.put("Botellas",botellas);
                    Monjitas.put("Monjitas",monjitas);
    
                    //Cargo en el lote todos los Mapas de datos
                    mLote.set(refPedido , Barriles, SetOptions.merge());
                    mLote.set(refPedido , Botellas, SetOptions.merge());
                    mLote.set(refPedido , Monjitas, SetOptions.merge());
    
                    //Cargo al lote informacion extra
                    mLote.set(refPedido, extra, SetOptions.merge());
    
    
    
                    //Subo el lote
                    mPedidoProgress.setVisibility(View.VISIBLE);
                    mLote.commit().addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
    
                            Toast.makeText(PedidoActivity.this, "Pedido Agregado",
                                    Toast.LENGTH_LONG).show();
    
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
    
                            Toast.makeText(PedidoActivity.this, "Error agregando el pedido al servidor",
                                    Toast.LENGTH_LONG).show();
    
    
                        }
                    });
    
                    // Bunch of code that is not relevant...
        }
    

    更新代码

     //BOTON REALIZAR PEDIDO
        mRealizarPedido = findViewById(R.id.pedido_boton_realizarPedido);
        mRealizarPedido.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                   // Bunch of code that is not relevant...
    
    
                    // esta aprobado o no , y el precio del pedido
                    extra.put("timestamp", FieldValue.serverTimestamp());
                    extra.put("aprobado", false);
    
                    calcularPrecio(editTextArray,nombres,posiciones);
    
                }
    
            }
        });
    

    更新calcularPrecio

    private void calcularPrecio(final EditText[] editTextArray, final String[] nombres, final List<Integer> posiciones) {
    
        mBaseDatos.collection("Precios").document("precios").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    
                if (task.isSuccessful()) {
    
    
                        Log.d("inside:  ", String.valueOf(extra.get("precio")));
                        updateDetails();
    
                    } else {
    
                        Log.d(TAG, "No existe el documento");
    
                    }
    
                } else {
    
                    Log.d(TAG, "get fallo con la exepción: ", task.getException());
    
                }
    
            }
        });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多