Java 方法在调用时不执行。火库
我有一个方法:calcularPrecio(),当我单击按钮时会调用该方法。该方法必须更新price(precio),并在TextView中设置文本。之后,我从同一个TextView中从方法外部检索文本,并将其放入批处理中以将其上载到Firestore 问题是在该方法完成之前在批中设置了价格,因此我在批中获得了旧的价格,然后TextView更新 我用调试器查看了一下。它开始执行这个方法,然后出去,然后返回来完成这个方法,我不知道为什么 这是按钮的代码:Java 方法在调用时不执行。火库,java,android,google-cloud-firestore,Java,Android,Google Cloud Firestore,我有一个方法:calcularPrecio(),当我单击按钮时会调用该方法。该方法必须更新price(precio),并在TextView中设置文本。之后,我从同一个TextView中从方法外部检索文本,并将其放入批处理中以将其上载到Firestore 问题是在该方法完成之前在批中设置了价格,因此我在批中获得了旧的价格,然后TextView更新 我用调试器查看了一下。它开始执行这个方法,然后出去,然后返回来完成这个方法,我不知道为什么 这是按钮的代码: mRealizarPedido =
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...
}
}
});
在输出以下各项之后:
Log.d("outside: ", String.valueOf(extra.get("precio")));
Log.d("inside: ", String.valueOf(extra.get("precio")));
应该是另一种方式!我错过了什么?如何获取更新后的价格以上载到批次
谢谢大家! 您必须在执行
onComplete
方法后调用OnCompleteListener
的方法,因此创建一个名为updateDetails()
的新方法,并在Log.d(“内部:
更新的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());
}
}
});
}
private void calcularPrecio(最终编辑文本[]编辑文本数组,最终字符串[]nombres,最终列表位置){
MBAOS.collection(“Precios”).document(“Precios”).get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Log.d(“inside:”,String.valueOf(extra.get(“precio”));
updateDetails();
}否则{
日志d(标签“不存在文件”);
}
}否则{
Log.d(标记“getfalloconlaexecpción:”,task.getException());
}
}
});
}
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);
}
}
});
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());
}
}
});
}