通过Firebase Android开发中的循环从子节点检索特定数据

时间:2019-05-26 12:42:52

标签: java android firebase firebase-realtime-database

我正在尝试从firebase数据库中获取特定值。请先查看代码。 它是数据库参考

DatabaseReference databasedeposit= FirebaseDatabase.getInstance().getReference("Deposit");


totalb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            databasedeposit.addListenerForSingleValueEvent (new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    abc.clear();
                    Integer total = 0;

                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        ADeposit bazar = ds.getValue(ADeposit.class);
                        Integer cost = Integer.valueOf(bazar.getAmount());
                        total = total + cost;
                        abc.add(bazar);

                    }

                    Query queryn=databasedeposit.orderByChild("name").equalTo("Rabbani");

                    queryn.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Integer n_total=0;

                            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                ADeposit bazar = ds.getValue(ADeposit.class);
                                Integer c = Integer.valueOf(bazar.getAmount());
                                n_total = n_total + c;
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });





                    DepositList adapter = new DepositList(admininterface.this,abc);
                    deposit.setAdapter(adapter);

                    AlertDialog.Builder dialog1 = new AlertDialog.Builder(admininterface.this);
                    dialog1.setTitle("Notification");
                    dialog1.setMessage("Total Deposit  : " +total );


                    dialog1.setIcon(R.drawable.ic_done_black_24dp);
                    dialog1.setNeutralButton(
                            "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {

                                    dialog.dismiss();

                                }
                            });
                    dialog1.show();

                }


                @Override
                public void onCancelled(DatabaseError databaseError) {}
            }); ;

        }
    });


}

现在总成本很容易收回。但是来自用户的特定数据(如此处的“ Rabbani”)用于检索其总金额,我创建了一个查询,但无法从类似对话框的部分访问n_total值。

看一下数据库结构:

firebase database

现在,我要从名称“ Rabbani”中扣除所有金额,因此就像对其他名称“ Sobuj”一样。这个过程对吗? 还是还有其他过程?检索Separe用户的总金额?

1 个答案:

答案 0 :(得分:0)

您不需要嵌套的侦听器即可获得Rabbani的花费。由于此数据已经存在于databasedeposit中,因此您可以在现有循环中使用if来跟踪该数据:

databasedeposit.addListenerForSingleValueEvent (new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        abc.clear();
        Integer total = 0;
        Integer n_total=0;

        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            ADeposit bazar = ds.getValue(ADeposit.class);
            Integer cost = Integer.valueOf(bazar.getAmount());
            total = total + cost;
            abc.add(bazar);

            if ("Rabbani".equals(ds.child("name").getValue(String.class)) {
                n_total = n_total + cost;
            }
        }

        System.out.println("total: "+total+", n_total: "+n_total);

        ...

    }


    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
}); ;