Ad
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:
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?
Ad
Answer
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
}
}); ;
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad