Adding up value and store in Firebase
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to build an app for vending machine simulator and I store the value in Firebase.
This is my Firebase data for coin denomination and drink quantity:
This is my first activity:
This is my second activity
I want to store coin entered by the customer in each denomination (e.g.: if customer enter 20 cent, the quantity for 20 cent will be added by 1 in Firebase and so on) but I don't know how to do this. Same problem happen for drink quantity (e.g.: If the amount = price or amount > price, the system should dispense the drink and deduct the quantity of drink by 1 in Firebase).
I understand the logic, but I don't know how to implement it in android development since I'm still learning and new to this.
Any help will be greatly appreciated. Thank you in advance.
This is my DrinkActivity.java
public class DrinkActivity extends AppCompatActivity {
Button terminateBtn;
ImageButton tenCent, twentyCent, fiftyCent, oneRinggit, slugCoin1, slugCoin2;
TextView amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
// Receive data
String name = getIntent().getExtras().getString("name");
String price = getIntent().getExtras().getString("price");
String image_url = getIntent().getExtras().getString("img");
// init view
TextView tv_name = findViewById(R.id.aa_name);
TextView tv_price = findViewById(R.id.aa_price);
ImageView img = findViewById(R.id.aa_thumbnail);
// setting values to each view
tv_name.setText(name);
tv_price.setText(String.valueOf(price));
RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
// set image using Glide
Glide.with(this).load(image_url).apply(requestOptions).into(img);
//Amount
amount = (TextView) findViewById(R.id.amountView);
//Coin Button
tenCent = (ImageButton) findViewById(R.id.cent10);
twentyCent = (ImageButton) findViewById(R.id.cent20);
fiftyCent = (ImageButton) findViewById(R.id.cent50);
oneRinggit = (ImageButton) findViewById(R.id.rm1);
slugCoin1 = (ImageButton) findViewById(R.id.unknown1);
slugCoin2 = (ImageButton) findViewById(R.id.unknown2);
terminateBtn = (Button) findViewById(R.id.terminateBtn);
//Terminate button
terminateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When terminate the transaction, it should return the amount that already has
//been inserted by the customer.
Intent i = new Intent(DrinkActivity.this,Home.class);
startActivity(i);
}
});
//10 cent button
tenCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.10");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//20 cent button
twentyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.20");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//50 cent button
fiftyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.50");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//RM 1 button
oneRinggit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 1.00");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 1 button
slugCoin1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 2 button
slugCoin2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
}
public void playSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.insert_coins);
mediaPlayer.start();
}
public void slugCoinSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.change);
mediaPlayer.start();
}
}
java android firebase firebase-realtime-database
add a comment |
I am trying to build an app for vending machine simulator and I store the value in Firebase.
This is my Firebase data for coin denomination and drink quantity:
This is my first activity:
This is my second activity
I want to store coin entered by the customer in each denomination (e.g.: if customer enter 20 cent, the quantity for 20 cent will be added by 1 in Firebase and so on) but I don't know how to do this. Same problem happen for drink quantity (e.g.: If the amount = price or amount > price, the system should dispense the drink and deduct the quantity of drink by 1 in Firebase).
I understand the logic, but I don't know how to implement it in android development since I'm still learning and new to this.
Any help will be greatly appreciated. Thank you in advance.
This is my DrinkActivity.java
public class DrinkActivity extends AppCompatActivity {
Button terminateBtn;
ImageButton tenCent, twentyCent, fiftyCent, oneRinggit, slugCoin1, slugCoin2;
TextView amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
// Receive data
String name = getIntent().getExtras().getString("name");
String price = getIntent().getExtras().getString("price");
String image_url = getIntent().getExtras().getString("img");
// init view
TextView tv_name = findViewById(R.id.aa_name);
TextView tv_price = findViewById(R.id.aa_price);
ImageView img = findViewById(R.id.aa_thumbnail);
// setting values to each view
tv_name.setText(name);
tv_price.setText(String.valueOf(price));
RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
// set image using Glide
Glide.with(this).load(image_url).apply(requestOptions).into(img);
//Amount
amount = (TextView) findViewById(R.id.amountView);
//Coin Button
tenCent = (ImageButton) findViewById(R.id.cent10);
twentyCent = (ImageButton) findViewById(R.id.cent20);
fiftyCent = (ImageButton) findViewById(R.id.cent50);
oneRinggit = (ImageButton) findViewById(R.id.rm1);
slugCoin1 = (ImageButton) findViewById(R.id.unknown1);
slugCoin2 = (ImageButton) findViewById(R.id.unknown2);
terminateBtn = (Button) findViewById(R.id.terminateBtn);
//Terminate button
terminateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When terminate the transaction, it should return the amount that already has
//been inserted by the customer.
Intent i = new Intent(DrinkActivity.this,Home.class);
startActivity(i);
}
});
//10 cent button
tenCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.10");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//20 cent button
twentyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.20");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//50 cent button
fiftyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.50");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//RM 1 button
oneRinggit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 1.00");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 1 button
slugCoin1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 2 button
slugCoin2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
}
public void playSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.insert_coins);
mediaPlayer.start();
}
public void slugCoinSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.change);
mediaPlayer.start();
}
}
java android firebase firebase-realtime-database
add a comment |
I am trying to build an app for vending machine simulator and I store the value in Firebase.
This is my Firebase data for coin denomination and drink quantity:
This is my first activity:
This is my second activity
I want to store coin entered by the customer in each denomination (e.g.: if customer enter 20 cent, the quantity for 20 cent will be added by 1 in Firebase and so on) but I don't know how to do this. Same problem happen for drink quantity (e.g.: If the amount = price or amount > price, the system should dispense the drink and deduct the quantity of drink by 1 in Firebase).
I understand the logic, but I don't know how to implement it in android development since I'm still learning and new to this.
Any help will be greatly appreciated. Thank you in advance.
This is my DrinkActivity.java
public class DrinkActivity extends AppCompatActivity {
Button terminateBtn;
ImageButton tenCent, twentyCent, fiftyCent, oneRinggit, slugCoin1, slugCoin2;
TextView amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
// Receive data
String name = getIntent().getExtras().getString("name");
String price = getIntent().getExtras().getString("price");
String image_url = getIntent().getExtras().getString("img");
// init view
TextView tv_name = findViewById(R.id.aa_name);
TextView tv_price = findViewById(R.id.aa_price);
ImageView img = findViewById(R.id.aa_thumbnail);
// setting values to each view
tv_name.setText(name);
tv_price.setText(String.valueOf(price));
RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
// set image using Glide
Glide.with(this).load(image_url).apply(requestOptions).into(img);
//Amount
amount = (TextView) findViewById(R.id.amountView);
//Coin Button
tenCent = (ImageButton) findViewById(R.id.cent10);
twentyCent = (ImageButton) findViewById(R.id.cent20);
fiftyCent = (ImageButton) findViewById(R.id.cent50);
oneRinggit = (ImageButton) findViewById(R.id.rm1);
slugCoin1 = (ImageButton) findViewById(R.id.unknown1);
slugCoin2 = (ImageButton) findViewById(R.id.unknown2);
terminateBtn = (Button) findViewById(R.id.terminateBtn);
//Terminate button
terminateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When terminate the transaction, it should return the amount that already has
//been inserted by the customer.
Intent i = new Intent(DrinkActivity.this,Home.class);
startActivity(i);
}
});
//10 cent button
tenCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.10");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//20 cent button
twentyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.20");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//50 cent button
fiftyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.50");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//RM 1 button
oneRinggit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 1.00");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 1 button
slugCoin1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 2 button
slugCoin2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
}
public void playSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.insert_coins);
mediaPlayer.start();
}
public void slugCoinSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.change);
mediaPlayer.start();
}
}
java android firebase firebase-realtime-database
I am trying to build an app for vending machine simulator and I store the value in Firebase.
This is my Firebase data for coin denomination and drink quantity:
This is my first activity:
This is my second activity
I want to store coin entered by the customer in each denomination (e.g.: if customer enter 20 cent, the quantity for 20 cent will be added by 1 in Firebase and so on) but I don't know how to do this. Same problem happen for drink quantity (e.g.: If the amount = price or amount > price, the system should dispense the drink and deduct the quantity of drink by 1 in Firebase).
I understand the logic, but I don't know how to implement it in android development since I'm still learning and new to this.
Any help will be greatly appreciated. Thank you in advance.
This is my DrinkActivity.java
public class DrinkActivity extends AppCompatActivity {
Button terminateBtn;
ImageButton tenCent, twentyCent, fiftyCent, oneRinggit, slugCoin1, slugCoin2;
TextView amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
// Receive data
String name = getIntent().getExtras().getString("name");
String price = getIntent().getExtras().getString("price");
String image_url = getIntent().getExtras().getString("img");
// init view
TextView tv_name = findViewById(R.id.aa_name);
TextView tv_price = findViewById(R.id.aa_price);
ImageView img = findViewById(R.id.aa_thumbnail);
// setting values to each view
tv_name.setText(name);
tv_price.setText(String.valueOf(price));
RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
// set image using Glide
Glide.with(this).load(image_url).apply(requestOptions).into(img);
//Amount
amount = (TextView) findViewById(R.id.amountView);
//Coin Button
tenCent = (ImageButton) findViewById(R.id.cent10);
twentyCent = (ImageButton) findViewById(R.id.cent20);
fiftyCent = (ImageButton) findViewById(R.id.cent50);
oneRinggit = (ImageButton) findViewById(R.id.rm1);
slugCoin1 = (ImageButton) findViewById(R.id.unknown1);
slugCoin2 = (ImageButton) findViewById(R.id.unknown2);
terminateBtn = (Button) findViewById(R.id.terminateBtn);
//Terminate button
terminateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When terminate the transaction, it should return the amount that already has
//been inserted by the customer.
Intent i = new Intent(DrinkActivity.this,Home.class);
startActivity(i);
}
});
//10 cent button
tenCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.10");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//20 cent button
twentyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.20");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//50 cent button
fiftyCent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 0.50");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//RM 1 button
oneRinggit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
amount.setText("RM 1.00");
playSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 1 button
slugCoin1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
//Slug coin 2 button
slugCoin2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DrinkActivity.this, "Invalid Coin!" , Toast.LENGTH_SHORT).show();
amount.setText("RM 0.00");
slugCoinSound();
}
}, 1000);
amount.setText("Checking...");
}
});
}
public void playSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.insert_coins);
mediaPlayer.start();
}
public void slugCoinSound() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.change);
mediaPlayer.start();
}
}
java android firebase firebase-realtime-database
java android firebase firebase-realtime-database
edited Jan 3 at 14:01
Frank van Puffelen
246k31392420
246k31392420
asked Jan 3 at 13:54
ChxkChxk
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You should use runTransaction
method of firebase. Here is an example code you can use for the your 20 Cent coins. The code for the other coins is similar:
public void onClick(View view) {
DatabaseReference quantityRef = ref.child("yoururl/Coins/1/quantity");
quantityRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(0);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
System.out.println("Value incremented");
}
});
}
You could also implement it in the addValueEventListener
but it is not safe against concurrency:
public void onClick(View view) {
DatabaseReference ref = youdatabaseRef.child("Coins/1");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int quantity = (int) dataSnapshot.child("quantity").getValue();
ref.child("quantity").setValue(quantity++);
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
In the code ref is the reference to your firebase database. You code it like this:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog");
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023702%2fadding-up-value-and-store-in-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use runTransaction
method of firebase. Here is an example code you can use for the your 20 Cent coins. The code for the other coins is similar:
public void onClick(View view) {
DatabaseReference quantityRef = ref.child("yoururl/Coins/1/quantity");
quantityRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(0);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
System.out.println("Value incremented");
}
});
}
You could also implement it in the addValueEventListener
but it is not safe against concurrency:
public void onClick(View view) {
DatabaseReference ref = youdatabaseRef.child("Coins/1");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int quantity = (int) dataSnapshot.child("quantity").getValue();
ref.child("quantity").setValue(quantity++);
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
In the code ref is the reference to your firebase database. You code it like this:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog");
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
add a comment |
You should use runTransaction
method of firebase. Here is an example code you can use for the your 20 Cent coins. The code for the other coins is similar:
public void onClick(View view) {
DatabaseReference quantityRef = ref.child("yoururl/Coins/1/quantity");
quantityRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(0);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
System.out.println("Value incremented");
}
});
}
You could also implement it in the addValueEventListener
but it is not safe against concurrency:
public void onClick(View view) {
DatabaseReference ref = youdatabaseRef.child("Coins/1");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int quantity = (int) dataSnapshot.child("quantity").getValue();
ref.child("quantity").setValue(quantity++);
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
In the code ref is the reference to your firebase database. You code it like this:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog");
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
add a comment |
You should use runTransaction
method of firebase. Here is an example code you can use for the your 20 Cent coins. The code for the other coins is similar:
public void onClick(View view) {
DatabaseReference quantityRef = ref.child("yoururl/Coins/1/quantity");
quantityRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(0);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
System.out.println("Value incremented");
}
});
}
You could also implement it in the addValueEventListener
but it is not safe against concurrency:
public void onClick(View view) {
DatabaseReference ref = youdatabaseRef.child("Coins/1");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int quantity = (int) dataSnapshot.child("quantity").getValue();
ref.child("quantity").setValue(quantity++);
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
In the code ref is the reference to your firebase database. You code it like this:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog");
You should use runTransaction
method of firebase. Here is an example code you can use for the your 20 Cent coins. The code for the other coins is similar:
public void onClick(View view) {
DatabaseReference quantityRef = ref.child("yoururl/Coins/1/quantity");
quantityRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(0);
} else {
mutableData.setValue(currentValue + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
System.out.println("Value incremented");
}
});
}
You could also implement it in the addValueEventListener
but it is not safe against concurrency:
public void onClick(View view) {
DatabaseReference ref = youdatabaseRef.child("Coins/1");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int quantity = (int) dataSnapshot.child("quantity").getValue();
ref.child("quantity").setValue(quantity++);
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
In the code ref is the reference to your firebase database. You code it like this:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog");
answered Jan 3 at 15:07
Code PopeCode Pope
1,49321638
1,49321638
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
add a comment |
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
It is not working sir. But, thank you
– Chxk
Jan 3 at 16:28
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
@Chxk is your url correct? What is not working? What kind of error do you get?
– Code Pope
Jan 3 at 17:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023702%2fadding-up-value-and-store-in-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown