【问题标题】:Get specific values from Firebase Realtime and collect their values从 Firebase Realtime 获取特定值并收集它们的值
【发布时间】:2021-12-06 10:12:43
【问题描述】:

如何从 Firebase Realtime 获取特定值并收集所有这些值以向用户显示销售列表中所有子项的总价值 android studio - 使用 java

从 Firebase Realtime 获取特定值并收集它们的值 android studio - 使用 java

public class SalesFragment extends Fragment {

TextView otherSalesValue, otherEarnsValue;

DatabaseReference databaseReference;

int sale;

String currentUserId;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_sales, container, false);

    currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    databaseReference = FirebaseDatabase.getInstance().getReference("sales");

    otherSalesValue = root.findViewById(R.id.other_sales_value);
    otherEarnsValue = root.findViewById(R.id.other_earns_value);

    databaseReference.child(currentUserId)
            .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
                for (DataSnapshot booksSnapshot : uniqueKeySnapshot.child("total").getChildren()) {
                    String sales = booksSnapshot.getValue(String.class);
                    try {
                        sale = Integer.parseInt(sales);
                    } catch (NumberFormatException ex) {
                        ex.printStackTrace();
                    }

                    sale = +sale;

                    otherEarnsValue.setText(String.valueOf(sale));
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return root;
}

}

【问题讨论】:

  • 您到底想计算什么?预期的结果是什么?
  • 我想总结所有“总”值

标签: android firebase firebase-realtime-database


【解决方案1】:

您读取的sales 节点下有两个动态子级别,因此您需要在getChildren() 上进行两个嵌套循环才能获得total 属性。

databaseReference.child(currentUserId)
        .addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
            for (DataSnapshot dateSnapshot : userSnapshot.getChildren()) {
                                                     // ? loop over all child nodes
                long totalValue = dateSnapshot.child("total").getValue(Long.class);
                                                     // ? get the total for this date

                sale = sale + totalValue; // ? add the daily total to the sum
            }
        }
        otherEarnsValue.setText(String.valueOf(sale));
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // ? never ignore errors
    }
});

请注意,如果您只需要每日总数的总和,那么读取每个客户端中的所有附加数据是非常浪费的。

考虑在数据库的更高级别节点中存储一个运行总计(可能在/sales_total),并在您更新每日总计时更新它。像这样将数据保存在多个位置在 NoSQL 数据库中很常见,这也是它们的读取操作规模如此之大的部分原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2017-12-26
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多