简体   繁体   中英

Retrieve specific data from child nodes through loop in firebase android development

I am trying to get specific value from firebase database.look at the code first. It's data base reference

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) {}
            }); ;

        }
    });


}

now the total cost is easily retrievable. but specific data from a user like in here "Rabbani" for retrive his total amount i create a Query but i cannot access n_total value from the portion like dialogue box.

Take a look database structure:

firebase数据库

Now i want, to get the sum off all amount from name "Rabbani" so do same like for other name "Sobuj". is this process is right? or is there any other process? to retrieve separe users total amount?

You don't need the nested listener to get Rabbani 's spending. Since this data is already present in databasedeposit , you can just use an if in your existing loop to track that:

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
    }
}); ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM