如何从Firebase节点获得所有值的总和?错误



请告诉我:我错在哪里?我试着应用你的例子,但我不能得到求和结果。我想把所有价格加起来,显示在TextView中。我会很感激你的帮助,我是新手。输入图片描述

MainActivity包com.example.testkorzne;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.EventListener;
public class MainActivity extends AppCompatActivity implements ValueEventListener{
private EditText edName, edPrice;
private DatabaseReference mDataBase;
private String USER_KEY = "User";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int total = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String amount = ds.child("price").getValue(String.class);
int value = ds.getValue(Integer.class);
total =+ value;
}
Log.d("TAG", String.valueOf(total) + " Rs.");
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + total);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
mDataBase.addValueEventListener(this);
}
private void init() {
edName = findViewById(R.id.edName);
edPrice = findViewById(R.id.edPrice);
mDataBase = FirebaseDatabase.getInstance().getReference(USER_KEY);
}
public void onClickSave(View view) {
String id = mDataBase.getKey();
String name = edName.getText().toString();
String price = edPrice.getText().toString();
User newUser = new User(id, name, price);
if (!TextUtils.isEmpty(name))
{
mDataBase.push().setValue(newUser);
} else {
Toast.makeText(this, "Warning Not Text", Toast.LENGTH_LONG).show();
}
}
public void onClickRead(View view) {
}
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
}

用户类包com.example.testkorzne;

public class User {
public String id;
public String name;
public String price;
public User() {
}
public User(String id, String name, String price) {
this.id = id;
this.name = name;
this.price = price;
}
}

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:onClick="onClickSave"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.312"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edPrice" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="onClickRead"
android:text="Read"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.312"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<EditText
android:id="@+id/edName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:ems="10"
android:hint="NAME"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck" />
<EditText
android:id="@+id/edPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="PRICE"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edName"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="124dp"
android:layout_height="121dp"
android:layout_marginStart="224dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>

要获取User节点中每个子节点下存在的所有价格字段的总和,请使用以下代码行:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = db.child("User");
userRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
int sum = 0;
for (DataSnapshot ds : task.getResult().getChildren()) {
String price = ds.child("price").getValue(String.class);
sum += Integer.valueOf(price);
}
Log.d("TAG", "sum: " + sum);
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});

日志的结果将是:

sum: 200

还请注意将价格存储为数字和而不是字符串,在这种情况下,上面的加法应该是这样的:

sum += price;

相关内容

  • 没有找到相关文章

最新更新