在firebase中添加qty值的总量需要什么样的方法? 这里我附上了firebase数据库截图。
据推测,列表和秒的数量之和为500 + 800 = 1300。所以我希望得到总和1300.下面是从firebase检索数据以存储在textview(@ + id / txt_total_value)上进行显示的代码。
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("Sreeauto");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int sum = 0;
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot dSnapshot : postSnapshot.getChildren()) {
Datastore upload = dSnapshot.getValue(Datastore.class);
mUploads.add(upload);
int value =
Integer.parseInt(dSnapshot.child("qty").getValue(String.class));
sum += value;
}
}
mAdapter = new Adapater(ViewProduction.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
mprogress.setVisibility(View.INVISIBLE);
txt_total_value.setText(sum);
}
Datastore.java
public class Datastore {
private String Date;
private String Shift;
private String Qty;
private String Incharge;
public Datastore(){
}
public Datastore(String date,String shift, String qty, String incharge){
Date = date;
Shift = shift;
Qty = qty;
Incharge = incharge;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getShift() {
return Shift;
}
public void setShift(String shift) {
Shift = shift;
}
public String getQty() {
return Qty;
}
public void setQty(String qty) {
Qty = qty;
}
public String getIncharge() {
return Incharge;
}
public void setIncharge(String incharge) {
Incharge = incharge;
}
}
ViewProduction.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.udayaj.sreeauto.ViewProduction">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="15dp"
android:text="Date"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp"
android:layout_toStartOf="@+id/incharge"
android:text="Shift"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="30dp"
android:text="Qty"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/incharge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/progress"
android:layout_marginStart="27dp"
android:text="Incharge"
android:textSize="20sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/incharge"
android:id="@+id/recyclerji">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/txt_total_value"
android:text="Total : "
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_total_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/textView3"
android:text="00000"
android:textSize="20sp" />
</RelativeLayout>
一个例子是有帮助的。谢谢。
答案 0 :(得分:0)
您似乎已经在循环数据,所以仍然需要的是提取值并将它们添加到一起:
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int sum = 0;
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot dSnapshot : postSnapshot.getChildren()) {
Datastore upload = dSnapshot.getValue(Datastore.class);
mUploads.add(upload);
int value = Integer.parseInt(dSnapshot.child("qty").getValue(String.class));
sum += value;
}
}
System.out.println(sum);
}
您的qty
课程中可能已经有Datastore
。但既然你没有分享这些代码,上面的内容并不依赖于此。
考虑将qty
的值存储为数字。这将使Integer.parseInt
变得不必要,并减少出现问题的机会。