![](/img/trans.png)
[英]ListAdapter items rendered incorrectly when LiveData onChanged() method is called. Possible issue with DiffUtil areContentsTheSame()?
[英]Method does not execute when called. FIRESTORE
我有一个方法: 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")));
应该是相反的方式! 我缺少什么? 如何获得更新的价格以上传到批次?
谢谢!
您必须在执行OnCompleteListener
的onComplete
方法之后调用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());
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.