[英]how to retrieve profit of all mobiles from Firebase Realtime database and sum all the values
I am creating an application of sale purchase I want to retrieve all the profit of sale mobiles and Sum the values to obtain the total earning.我正在创建一个销售购买应用程序,我想检索销售手机的所有利润并对这些值求和以获得总收入。
mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
List<Float> list = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
list.add(Float.valueOf(Float.parseFloat(ds.child("profit").getValue().toString())));
double sum = 0.0;
for (Float d : list) {
double floatValue = (double) d.floatValue();
Double.isNaN(floatValue);
sum += floatValue;
}
Toast.makeText(RecordsActivity.this, "Earning = " + sum, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(RecordsActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
For starters I would suggest that you send profits to the database as ints.对于初学者,我建议您将利润作为整数发送到数据库。 Then you can find the total sum of the figures under the nodes profits in a data snapshot as shown below
然后您可以在数据快照中找到节点利润下的数字总和,如下所示
@Override
public void onDataChange(@NonNull DataSnapshot
dataSnapshot) {
int sum=0;
for (DataSnapshot ds1:dataSnapshot.getChildren()){
int a=ds1.child("profit").getValue(Integer.class);
sum+=b;
int i = sum;
String value = String.valueOf(i);
Toast.makeText(RecordsActivity.this, value,
Toast .LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.