【发布时间】: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