java firebase数据库的总值
需要什么样的方法来增加firebase中的总数量价值? 这里我附上了firebase数据库截图
据推测,list和second的数量之和为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);
}
数据存储。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;
}
}
观看制作。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.udayaj.sreeauto.ViewProduction">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentStart="true"
安卓:layout_alignParentTop="true"
安卓:layout_marginStart="15dp"
安卓:text="Date"
安卓:textStyle="bold"
安卓:textSize="20sp"
安卓:id="@+id/textView2" />
<TextView
安卓:id="@+id/textView4"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_marginEnd="16dp"
安卓:layout_toStartOf="@+id/incharge"
安卓:text="Shift"
安卓:textSize="20sp"
安卓:textStyle="bold" />
<TextView
安卓:id="@+id/textView3"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentEnd="true"
安卓:layout_alignParentTop="true"
安卓:layout_marginEnd="30dp"
安卓:text="Qty"
安卓:textSize="20sp"
安卓:textStyle="bold" />
<TextView
安卓:id="@+id/incharge"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_alignStart="@+id/progress"
安卓:layout_marginStart="27dp"
安卓:text="Incharge"
安卓:textSize="20sp"
安卓:textStyle="bold" />
<ProgressBar
安卓:id="@+id/progress"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_centerInParent="true"/>
<安卓.support.v7.widget.RecyclerView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_below="@+id/incharge"
安卓:id="@+id/recyclerji">
</安卓.support.v7.widget.RecyclerView>
<TextView
安卓:id="@+id/txt_total"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true"
安卓:layout_toStartOf="@+id/txt_total_value"
安卓:text="Total : "
安卓:textSize="20sp"
安卓:textStyle="bold" />
<TextView
安卓:id="@+id/txt_total_value"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true"
安卓:layout_alignStart="@+id/textView3"
安卓:text="00000"
安卓:textSize="20sp" />
</RelativeLayout>
举个例子会很有帮助。谢谢
# 1 楼答案
您似乎已经在循环处理数据,所以仍然需要提取值并将它们相加:
在
Datastore
类中可能已经有qty
。但是,由于您没有共享该代码,以上内容并不依赖于此考虑将^ {CD1>}的值存储为一个数字。这将使
Integer.parseInt
变得不必要,并减少出现问题的可能性