How to save users score in firebase and retrieve it in real-time in Android studio
I am creating an app in which points increases on button click and those points should be saved to firebase. I managed to save those data to firebase. But when I destroy my app and open it again the points value is showing same but after clicking button. It again starts from 0.
For example: every time on button click the points value increases to 10 points. Now when I completely destroy the app and open it again, the points value shows same, but when button clicked it again starts from initial condition.
Here is my code
int amount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button_claim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amount = amount + 100;
textView_points.setText(String.valueOf(amount));
databaseReference.setValue(textView_points.getText().toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() == null) {
finish();
Intent main = new Intent(this,MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
textView_points.setText(dataSnapshot.getValue(String.class));
databaseReference.setValue(textView_points.getText().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Main2Activity.this,"error",Toast.LENGTH_LONG).show();
}
});
}
Before destroying the app
see the image please, points increasing on button click before destroying the app and uploading to database
please see image 1
Opened app again(after closing) it is showing the same updated point
please see image 2
Now when i click claim it returns back to 100
please see image number 3
please help me on this problem, and i am a newbie
Thanks
android firebase firebase-realtime-database
add a comment |
I am creating an app in which points increases on button click and those points should be saved to firebase. I managed to save those data to firebase. But when I destroy my app and open it again the points value is showing same but after clicking button. It again starts from 0.
For example: every time on button click the points value increases to 10 points. Now when I completely destroy the app and open it again, the points value shows same, but when button clicked it again starts from initial condition.
Here is my code
int amount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button_claim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amount = amount + 100;
textView_points.setText(String.valueOf(amount));
databaseReference.setValue(textView_points.getText().toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() == null) {
finish();
Intent main = new Intent(this,MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
textView_points.setText(dataSnapshot.getValue(String.class));
databaseReference.setValue(textView_points.getText().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Main2Activity.this,"error",Toast.LENGTH_LONG).show();
}
});
}
Before destroying the app
see the image please, points increasing on button click before destroying the app and uploading to database
please see image 1
Opened app again(after closing) it is showing the same updated point
please see image 2
Now when i click claim it returns back to 100
please see image number 3
please help me on this problem, and i am a newbie
Thanks
android firebase firebase-realtime-database
Please post your code so we can help you
– Rosário Pereira Fernandes
Jan 17 '18 at 18:27
Please add the code you have coded so far.
– Alex Mamo
Jan 17 '18 at 19:11
add a comment |
I am creating an app in which points increases on button click and those points should be saved to firebase. I managed to save those data to firebase. But when I destroy my app and open it again the points value is showing same but after clicking button. It again starts from 0.
For example: every time on button click the points value increases to 10 points. Now when I completely destroy the app and open it again, the points value shows same, but when button clicked it again starts from initial condition.
Here is my code
int amount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button_claim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amount = amount + 100;
textView_points.setText(String.valueOf(amount));
databaseReference.setValue(textView_points.getText().toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() == null) {
finish();
Intent main = new Intent(this,MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
textView_points.setText(dataSnapshot.getValue(String.class));
databaseReference.setValue(textView_points.getText().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Main2Activity.this,"error",Toast.LENGTH_LONG).show();
}
});
}
Before destroying the app
see the image please, points increasing on button click before destroying the app and uploading to database
please see image 1
Opened app again(after closing) it is showing the same updated point
please see image 2
Now when i click claim it returns back to 100
please see image number 3
please help me on this problem, and i am a newbie
Thanks
android firebase firebase-realtime-database
I am creating an app in which points increases on button click and those points should be saved to firebase. I managed to save those data to firebase. But when I destroy my app and open it again the points value is showing same but after clicking button. It again starts from 0.
For example: every time on button click the points value increases to 10 points. Now when I completely destroy the app and open it again, the points value shows same, but when button clicked it again starts from initial condition.
Here is my code
int amount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button_claim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amount = amount + 100;
textView_points.setText(String.valueOf(amount));
databaseReference.setValue(textView_points.getText().toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() == null) {
finish();
Intent main = new Intent(this,MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
textView_points.setText(dataSnapshot.getValue(String.class));
databaseReference.setValue(textView_points.getText().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Main2Activity.this,"error",Toast.LENGTH_LONG).show();
}
});
}
Before destroying the app
see the image please, points increasing on button click before destroying the app and uploading to database
please see image 1
Opened app again(after closing) it is showing the same updated point
please see image 2
Now when i click claim it returns back to 100
please see image number 3
please help me on this problem, and i am a newbie
Thanks
android firebase firebase-realtime-database
android firebase firebase-realtime-database
edited Jan 18 '18 at 15:46
Hash
asked Jan 17 '18 at 18:22
HashHash
42111
42111
Please post your code so we can help you
– Rosário Pereira Fernandes
Jan 17 '18 at 18:27
Please add the code you have coded so far.
– Alex Mamo
Jan 17 '18 at 19:11
add a comment |
Please post your code so we can help you
– Rosário Pereira Fernandes
Jan 17 '18 at 18:27
Please add the code you have coded so far.
– Alex Mamo
Jan 17 '18 at 19:11
Please post your code so we can help you
– Rosário Pereira Fernandes
Jan 17 '18 at 18:27
Please post your code so we can help you
– Rosário Pereira Fernandes
Jan 17 '18 at 18:27
Please add the code you have coded so far.
– Alex Mamo
Jan 17 '18 at 19:11
Please add the code you have coded so far.
– Alex Mamo
Jan 17 '18 at 19:11
add a comment |
2 Answers
2
active
oldest
votes
This is how you set a value in your Firebase database:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("score").setValue(1);
Assuming that the your score
field is of type Integer, to solve this, please use the following method:
public static void setScore(String operation) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
scoreRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer score = mutableData.getValue(Integer.class);
if (score == null) {
return Transaction.success(mutableData);
}
if (operation.equals("increaseScore")) {
mutableData.setValue(score + 1);
} else if (operation.equals("decreaseScore")){
mutableData.setValue(score - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {}
});
}
For this, I recommend you definitely use transactions
. You will avoid wrong results if users are trying to increase/decrease the score in the same time. So as a conclusion, call this method accordingly to your increase/decrease operation.
This is how you can read it:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Integer score = ds.getValue(Integer.class);
Log.d("TAG", score + "");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
|
show 6 more comments
You need to first get the value of previous score and then add it with the current score and then update the final value in the firebase database.
question_score.child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String previousScore = dataSnapshot.getValue(String.class);
if (previousScore!=null){
int finalScore = Integer.parseInt(previousScore) + current_score;
question_score.child("score").setValue(String.valueOf(finalScore));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
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%2f48307610%2fhow-to-save-users-score-in-firebase-and-retrieve-it-in-real-time-in-android-stud%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is how you set a value in your Firebase database:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("score").setValue(1);
Assuming that the your score
field is of type Integer, to solve this, please use the following method:
public static void setScore(String operation) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
scoreRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer score = mutableData.getValue(Integer.class);
if (score == null) {
return Transaction.success(mutableData);
}
if (operation.equals("increaseScore")) {
mutableData.setValue(score + 1);
} else if (operation.equals("decreaseScore")){
mutableData.setValue(score - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {}
});
}
For this, I recommend you definitely use transactions
. You will avoid wrong results if users are trying to increase/decrease the score in the same time. So as a conclusion, call this method accordingly to your increase/decrease operation.
This is how you can read it:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Integer score = ds.getValue(Integer.class);
Log.d("TAG", score + "");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
|
show 6 more comments
This is how you set a value in your Firebase database:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("score").setValue(1);
Assuming that the your score
field is of type Integer, to solve this, please use the following method:
public static void setScore(String operation) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
scoreRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer score = mutableData.getValue(Integer.class);
if (score == null) {
return Transaction.success(mutableData);
}
if (operation.equals("increaseScore")) {
mutableData.setValue(score + 1);
} else if (operation.equals("decreaseScore")){
mutableData.setValue(score - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {}
});
}
For this, I recommend you definitely use transactions
. You will avoid wrong results if users are trying to increase/decrease the score in the same time. So as a conclusion, call this method accordingly to your increase/decrease operation.
This is how you can read it:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Integer score = ds.getValue(Integer.class);
Log.d("TAG", score + "");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
|
show 6 more comments
This is how you set a value in your Firebase database:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("score").setValue(1);
Assuming that the your score
field is of type Integer, to solve this, please use the following method:
public static void setScore(String operation) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
scoreRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer score = mutableData.getValue(Integer.class);
if (score == null) {
return Transaction.success(mutableData);
}
if (operation.equals("increaseScore")) {
mutableData.setValue(score + 1);
} else if (operation.equals("decreaseScore")){
mutableData.setValue(score - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {}
});
}
For this, I recommend you definitely use transactions
. You will avoid wrong results if users are trying to increase/decrease the score in the same time. So as a conclusion, call this method accordingly to your increase/decrease operation.
This is how you can read it:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Integer score = ds.getValue(Integer.class);
Log.d("TAG", score + "");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);
This is how you set a value in your Firebase database:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("score").setValue(1);
Assuming that the your score
field is of type Integer, to solve this, please use the following method:
public static void setScore(String operation) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
scoreRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer score = mutableData.getValue(Integer.class);
if (score == null) {
return Transaction.success(mutableData);
}
if (operation.equals("increaseScore")) {
mutableData.setValue(score + 1);
} else if (operation.equals("decreaseScore")){
mutableData.setValue(score - 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {}
});
}
For this, I recommend you definitely use transactions
. You will avoid wrong results if users are trying to increase/decrease the score in the same time. So as a conclusion, call this method accordingly to your increase/decrease operation.
This is how you can read it:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Integer score = ds.getValue(Integer.class);
Log.d("TAG", score + "");
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);
edited May 19 '18 at 14:07
answered Jan 17 '18 at 19:39
Alex MamoAlex Mamo
44.6k82862
44.6k82862
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
|
show 6 more comments
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
sorry but i am very new to this so dont know much about above code please see above code i have given
– Hash
Jan 18 '18 at 15:49
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
Have you tried my code? Here you can find everything about Firebase transaction. Give it a try, will work.
– Alex Mamo
Jan 18 '18 at 15:51
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
yes i tried by removing advalueremoving method from my code but not working(i added your code but dont know why it is not working)
– Hash
Jan 18 '18 at 15:54
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
What is the error? Paste it here.
– Alex Mamo
Jan 18 '18 at 15:57
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
no errors but nothing is shown on textview nor saving to firebase or i think i am implementing it in wrong way
– Hash
Jan 18 '18 at 16:26
|
show 6 more comments
You need to first get the value of previous score and then add it with the current score and then update the final value in the firebase database.
question_score.child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String previousScore = dataSnapshot.getValue(String.class);
if (previousScore!=null){
int finalScore = Integer.parseInt(previousScore) + current_score;
question_score.child("score").setValue(String.valueOf(finalScore));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
add a comment |
You need to first get the value of previous score and then add it with the current score and then update the final value in the firebase database.
question_score.child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String previousScore = dataSnapshot.getValue(String.class);
if (previousScore!=null){
int finalScore = Integer.parseInt(previousScore) + current_score;
question_score.child("score").setValue(String.valueOf(finalScore));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
add a comment |
You need to first get the value of previous score and then add it with the current score and then update the final value in the firebase database.
question_score.child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String previousScore = dataSnapshot.getValue(String.class);
if (previousScore!=null){
int finalScore = Integer.parseInt(previousScore) + current_score;
question_score.child("score").setValue(String.valueOf(finalScore));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
You need to first get the value of previous score and then add it with the current score and then update the final value in the firebase database.
question_score.child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String previousScore = dataSnapshot.getValue(String.class);
if (previousScore!=null){
int finalScore = Integer.parseInt(previousScore) + current_score;
question_score.child("score").setValue(String.valueOf(finalScore));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
answered Jan 17 '18 at 18:49
DHANANJAY KUMARDHANANJAY KUMAR
458
458
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
add a comment |
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
Thanks please see my code and tell me some idea to implement in correct way , the code you gave is not working for me or i think i am doing something wrong
– Hash
Jan 18 '18 at 15:50
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%2f48307610%2fhow-to-save-users-score-in-firebase-and-retrieve-it-in-real-time-in-android-stud%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
Please post your code so we can help you
– Rosário Pereira Fernandes
Jan 17 '18 at 18:27
Please add the code you have coded so far.
– Alex Mamo
Jan 17 '18 at 19:11