Reputation: 25
I'm new at android programming as well as firebase
. first of all look at the picture
Details image
In there I want to get all the "cost" part in total(sum). I've created the reference like that
DatabaseReference databaseBazars=firebaseDatabase.getInstance().getReference("BazarList");
now what procedure I can follow to get sum and show it in list view or in a toast message).
I've done this to get all the data from that(BazarList) node . and it perfectly showing on the 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) {
}
});
now, how can I get the sum part of the cost. anyone, please help me
here is the 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;
}
}
Upvotes: 2
Views: 7164
Reputation: 491
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) {
}
})
Upvotes: 0
Reputation: 138824
To solve this, please use the following code:
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) {}
});
The output will be: 59512
.
Upvotes: 1
Reputation: 1471
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){
}
});
Upvotes: 0
Reputation: 3699
when you have bazarList
with items from firebase then simply iterate over list and do sum
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) {
}
});
below is code which will do the trick
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;
}
Upvotes: 0