我有一个方法: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")));
应该是相反的方式!我想念什么?如何获得更新的价格以上传到批次?
谢谢!
最佳答案
您必须在执行onComplete
的OnCompleteListener
方法之后调用method,因此创建一个名为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());
}
}
});
}