Reputation: 791
I am trying to update the quantity of a product in the database.Whenever I Add or Deduct the quantity, the change only reflects on my app but not updated in the Firebase! Any Help Will be greatly appreciated!
Here is a picture of the app:
Here is my Code:
public class Edit extends AppCompatActivity {
String myKey = null;
private Button removebutton, plus, minus;
private int counterValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
removebutton = (Button) findViewById(R.id.item_detail_delete);
plus = (Button) findViewById(R.id.item_quantity_add);
minus = (Button) findViewById(R.id.item_quantity_remove);
final DatabaseReference database;
myKey = getIntent().getExtras().getString("value");
database = FirebaseDatabase.getInstance().getReference().child("All Barcodes");
database.child(myKey).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView Barcode=(TextView) findViewById(R.id.Barcode);
TextView Pname=(TextView) findViewById(R.id.Pname);
TextView EntryDate=(TextView) findViewById(R.id.EntryDate);
TextView ExpiryDate=(TextView) findViewById(R.id.ExpiryDate);
final TextView Quantity=(TextView) findViewById(R.id.Quantity);
String barcode = (String) dataSnapshot.child("barcode").getValue();
String pname = (String) dataSnapshot.child("pname").getValue();
String date = (String) dataSnapshot.child("date").getValue();
String expiration = (String) dataSnapshot.child("expiration").getValue();
String quantity = (String) dataSnapshot.child("quantity").getValue();
Barcode.setText(barcode);
Pname.setText(pname);
EntryDate.setText(date);
ExpiryDate.setText(expiration);
Quantity.setText(quantity);
counterValue = Integer.valueOf(quantity);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counterValue ++;
Quantity.setText(String.valueOf(counterValue));
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counterValue --;
Quantity.setText(String.valueOf(counterValue));
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
removebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
database.child(myKey).removeValue();
Intent intent = new Intent(Edit.this,Inventory.class);
startActivity(intent);
}
});
}
Upvotes: 0
Views: 3092
Reputation: 3050
You should update the related field in the database yourself:
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counterValue++;
Quantity.setText(String.valueOf(counterValue));
database.child(myKey).child("quantity").setValue(String.valueOf(counterValue));
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counterValue--;
Quantity.setText(String.valueOf(counterValue));
database.child(myKey).child("quantity").setValue(String.valueOf(counterValue));
}
});
Upvotes: 2
Reputation: 244
The easiest way is to set keep sync to true like that:
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
This is from the firebase documentation
https://firebase.google.com/docs/database/android/offline-capabilities
Upvotes: 0