How do I multiply the value of quan (which is quantity) to price
I need to get the total of the price depending on the quantity that is ordered.
Thank you!!! :)
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
newForm.child("Food").setValue(food);
newForm.child("Quantity").setValue(quan);
newForm.child("Price").setValue(price);
newForm.child("Table").setValue(dataSnapshot.child("Name").getValue()).addOnCompleteListener(new OnCompleteListener<Void>()
{
#Override
public void onComplete(#NonNull Task<Void> task)
{
Toast.makeText(FoodDetail.this, "Submitted", Toast.LENGTH_SHORT).show();
//startActivity(new Intent(FoodDetail.this, Menu.class));
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
add this line to calculate total price.
newForm.child("Total_Price").setValue(quan*price);
Related
I have a button to delete amounts, and I have a textview with total amount, but when I delete amounts the textview with the total does not update, it only does if I go to another activity and come back, why is it? any ideas?
public void onClick(View view) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child(postId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
Expense expenseBackUp = new Expense(item, category, date, postId, amount);
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Snackbar.make(rv, item + " has been deleted", Snackbar.LENGTH_LONG).setAction("UNDO", new View.OnClickListener() {
#Override
public void onClick(View view) {
reference.child(postId).setValue(expenseBackUp);
}
}).show();
} else {
Toast.makeText(ExpensesActivity.this, "Something went wrong, try again" + task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
dialog.dismiss();
}
I wish to delete specific user with phone number X from my DB. And insted my code deletes all of them.
Here is my DB:
Code:
private static void deleteUser(final Context context, final String phoneNumber) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
Query usersQuery = databaseReference.orderByChild("phone").equalTo(phoneNumber);
usersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Log.d(TAG, "Deleted");
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}
});
}
Maybe it is not clear with my comment, so I am putting this as an answer, the full code that should not be having the problem you're facing, should look something like this:
usersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {
ds.getRef().removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Log.d(TAG, "Deleted");
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}
});
That's because you're using a singleValueEventListener. If the query matches multiple children, it returns a list of all those children.
Even if there's only a single matches child, it's still a list of one. And since you're calling getRef() on that list, you get the key of the location where you ran the query.
I have issues on nested queries I'm trying to do using firebase, seems like the first query (activity1) doesn't wait until the second (activity2) and third query (activity3) finished running, this might return NULL value from the first query. Please look at my sample for more understanding, I've been stuck here for days trying all kind of method but it just wont work. :(
Query query_1= reference.child("Users").child("Room")
.child("Profile");
query_1.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//ACTIVITY 1
Query query_2 = reference.child("Users").child(Room)
.child("Receiver").child("id");
query_2.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//ACTIVITY 2
Query query_3 = reference.child("chatrooms").child("Room")
.child("Creator").child("id");
query_3.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//ACTIVITY 3
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I´m using Firebase and in the login screen I have a method that it´s called when the user put the correct login credentials(that method start a Firebase thread in order to get all the info I need for the main screen), and when the main screen opened I need to wait until that method finish, I need some kind of flag or something, let me paste you my code:
Login.java:
mProgress.setMessage("Verificando Datos Usuario...");
mProgress.show();
mAuth.signInWithEmailAndPassword(usuario, pass)
.addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(Login.this, "Los Datos administrados no son correctos",
Toast.LENGTH_SHORT).show();
mProgress.dismiss();
} else{
Toast.makeText(Login.this, "Bienvenido... ",
Toast.LENGTH_SHORT).show();
AdminFirebase.loadAdmin(task.getResult().getUser());
mProgress.dismiss();
Intent intento=new Intent(Login.this, Principal.class);
startActivity(intento);
}
}
});
AdminFirebase.java:
public static void loadAdmin(FirebaseUser user){
DatabaseReference userRef=adminRef.child(user.getUid());
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Administrador administrador=dataSnapshot.getValue(Administrador.class);
Sesion.admin=administrador;
Sesion.admin.loadTaxis();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
the method I need to wait to finish to run it´s on the class Taxifirebase.java:
public static void loadTaxis(List<String> chapas){
for (String chapa :chapas) {
DatabaseReference taxiref=childRef.child(chapa);
taxiref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Taxi taxi=dataSnapshot.getValue(Taxi.class);
Sesion.taxis.add(taxi);
}
}
Thanks in advance.
If the flag is for you can use Log class.
If you need for the logic of program use a boolean and change to true when you need. Later you can check the value.
I want to Delete Specific value from Firebase Database following is Database which i have stored
Now, I want to Delete Following Values
fname: "John"
lname: "wick"
Here, What i have try to delete specific value from Firebase..
DatabaseReference demo = mReference.child("demo").orderByValue().equalTo("John").getRef();
demo.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this will delete all values from database...
Question : How to delete that specific value??? what i have to do for that
Any help will be appreciated.
Instead of using addListenerForSingleValueEventtry using addChildEventListener as below,
Query queryRef = mReference.child("demo").orderByChild("fname").equalTo("John");
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
snapshot.getRef().setValue(null);
}
});
hi try using this code,
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});
You can try it:
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});
Well Thanks to all, After Modification with lots of answer and query.. This is what worked with me.
mReference.child("demo").orderByChild("fname").equalTo("John").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
//also work
//dataSnapshot.getRef().setValue(null);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this code. Hopefully this will help you
Thanks
database.getReference().child("Groups").child(groupItem.getGroupkey()).child("members").child(groupMember.getKey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
new CustomMessage(GroupDetailsActivity.this, "Successfully deleted");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Customlog.showlogD("ERROR_MSG",e.getMessage());
}
});
Database structure: https://i.stack.imgur.com/Lw3Ia.png