我是android编程以及firebase
的新手。首先看图片
详情图片
在那里,我希望得到所有“成本”部分(总和)。我已经创建了像
这样的引用DatabaseReference databaseBazars=firebaseDatabase.getInstance().getReference("BazarList");
现在我可以遵循什么程序来获得总和并在列表视图或吐司消息中显示它。
我这样做是为了从该(BazarList)节点获取所有数据。它完美地显示在listview
上。
databaseBazars.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
bazarList.clear();
for( DataSnapshot ds :dataSnapshot.getChildren()) {
Bazar bazar = ds.getValue(Bazar.class);
bazarList.add(bazar);
}
BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
listViewBazars.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
现在,我如何得到费用的总和部分。有人,请帮帮我
这是Bazar Class
public class Bazar {
String bid;
String date;
String cost;
String name;
String item;
public Bazar(){
}
public Bazar(String bid, String date, String cost, String name,String item) {
this.bid = bid;
this.date = date;
this.cost = cost;
this.name = name;
this.item= item;
}
public String getBid() {
return bid;
}
public String getDate() {
return date;
}
public String getCost() {
return cost;
}
public String getName() {
return name;
}
public String getItem() {
return item;
}
}
答案 0 :(得分:1)
要解决此问题,请使用以下代码:
databaseBazars.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
bazarList.clear();
Integer total = 0;
for( DataSnapshot ds :dataSnapshot.getChildren()) {
Bazar bazar = ds.getValue(Bazar.class);
Integer cost = Integer.valueOf(bazar.getCost());
total = total + cost;
bazarList.add(bazar);
}
BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
listViewBazars.setAdapter(adapter);
Log.d("TAG", total + "");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
输出结果为:59512
。
答案 1 :(得分:0)
当你有bazarList
来自firebase的项目时,只需遍历列表并进行求和
databaseBazars.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
bazarList.clear();
for( DataSnapshot ds :dataSnapshot.getChildren()) {
Bazar bazar = ds.getValue(Bazar.class);
bazarList.add(bazar);
}
BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
listViewBazars.setAdapter(adapter);
}
double sum=getSum(); // at this moment your list have results so call getSum to get SUM
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
下面是代码,它可以解决这个问题
private double getSum()
{
double sum;
for( int i = 0; i < bazarList.size(); i++)
{
sum += bazarList.get(i).getCost; // Assuming Bazar class has getCost Method
}
return sum;
}
答案 2 :(得分:0)
databaseBazars.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange (DataSnapshot dataSnapshot){
bazarList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Bazar bazar = ds.getValue(Bazar.class);
bazarList.add(bazar);
}
// Iterate and add all costs to sum
double sum = 0;
for (Bazar b : bazarList) {
sum += Double.parseDouble(b.getCost())
}
BazarList adapter = new BazarList(Bazar_Data_Input.this, bazarList);
listViewBazars.setAdapter(adapter);
}
@Override
public void onCancelled (DatabaseError databaseError){
}
});
答案 3 :(得分:0)
mReference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (i in snapshot.children) {
val item = i.getValue(CartItem::class.java)
cartList!!.add(item!!)
}
var sum = 0.0
for (itm: CartItem in cartList!!) {
sum += itm.itemPrice.toDouble()
}
total_value.text = sum.toString()
//Initialise my adapter
mRecyclerView.adapter = CartItemsAdapter(applicationContext, cartList!!)
}
}
override fun onCancelled(error: DatabaseError) {
}
})