Returning value from callback in Java
So in my app I have 2 players in a game. Player 1 is assigned either heads or tails, and then player 2 is to be assigned the side that player 1 is not. i.e. if player 1 is heads, then player 2 is tails, and vice versa.
To check to see which side player 1 is before assigning a side to player 2, I have a callback setup to get which side player 1 has been assigned. And the callback looks like this.
public interface Player1SideCallback {
void onCallback(String side);
}
private void player1Side(final Player1SideCallback callback) {
FCGames.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
String player1Side = dataSnapshot.child(currentGameID).child("HeadsOrTails").getValue().toString();
callback.onCallback(player1Side);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
It's a pretty basic callback.
Now, I am when a player clicks the join button they are assigned to the game as player 2 using this snippet:
adapter = new MyRecyclerViewAdapter(this, openGames, new MyRecyclerViewAdapter.OnButtonClickListener() {
@Override public void onButtonClick(String id, final int position) {
String gameID = openGames.get(position);
second = new User(userName, 0, getSide2(), FirebaseAuth.getInstance().getUid(), gameID, false);
gm.setUser2(second);
FCGames.child(gameID).child("user2").setValue(second);
HomePage.setCurrentGame(gameID);
HomePage.setUserPosition(2);
}});
enter code here
As you can see, I have left line spaces around the variable second because that is where the new player is actually being created. And where it says getSide2() it is assigning a side to the player 2.
But, I am struggling to get this right. I call my callback in a method called getSide2() to try to properly assign the right side to user 2
private String getSide2() {
player1Side(new Player1SideCallback() {
@Override
public void onCallback(String side) {
if(side.equals("Heads")) {
String side2 = "Tails";
} else {
String side2 = "Heads";
}
}
});
}
So I want to assign the player2 the side with the variable side2 but I am not sure how to return this value from a method using a callback.
How can I use this type of logic to properly assign the correct side to user 2?
java
add a comment |
So in my app I have 2 players in a game. Player 1 is assigned either heads or tails, and then player 2 is to be assigned the side that player 1 is not. i.e. if player 1 is heads, then player 2 is tails, and vice versa.
To check to see which side player 1 is before assigning a side to player 2, I have a callback setup to get which side player 1 has been assigned. And the callback looks like this.
public interface Player1SideCallback {
void onCallback(String side);
}
private void player1Side(final Player1SideCallback callback) {
FCGames.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
String player1Side = dataSnapshot.child(currentGameID).child("HeadsOrTails").getValue().toString();
callback.onCallback(player1Side);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
It's a pretty basic callback.
Now, I am when a player clicks the join button they are assigned to the game as player 2 using this snippet:
adapter = new MyRecyclerViewAdapter(this, openGames, new MyRecyclerViewAdapter.OnButtonClickListener() {
@Override public void onButtonClick(String id, final int position) {
String gameID = openGames.get(position);
second = new User(userName, 0, getSide2(), FirebaseAuth.getInstance().getUid(), gameID, false);
gm.setUser2(second);
FCGames.child(gameID).child("user2").setValue(second);
HomePage.setCurrentGame(gameID);
HomePage.setUserPosition(2);
}});
enter code here
As you can see, I have left line spaces around the variable second because that is where the new player is actually being created. And where it says getSide2() it is assigning a side to the player 2.
But, I am struggling to get this right. I call my callback in a method called getSide2() to try to properly assign the right side to user 2
private String getSide2() {
player1Side(new Player1SideCallback() {
@Override
public void onCallback(String side) {
if(side.equals("Heads")) {
String side2 = "Tails";
} else {
String side2 = "Heads";
}
}
});
}
So I want to assign the player2 the side with the variable side2 but I am not sure how to return this value from a method using a callback.
How can I use this type of logic to properly assign the correct side to user 2?
java
why you have putted callback code to method its should be global or implemented by class level.
– Chetan Joshi
Nov 22 '18 at 6:16
@ChetanJoshi because I only want the value to be calledback whenever I need it to be (in this case when I call the method). How can I use the callback differently so that I can just get the value from the callback when I need it?
– CoderKlipto
Nov 22 '18 at 6:18
its not working as you think , because whenever callback comes method will not be triggering it self. And as you want callback only be called when onSuccess() called from your first code.
– Chetan Joshi
Nov 22 '18 at 6:24
@ChetanJoshi Okay so say I place the callback right above myadapterin the onCreate() method. How would I pull the valueside2out and then use it in myadapterto make user2?
– CoderKlipto
Nov 22 '18 at 6:27
Not in OnCreate() , its no need to add callback code under any block of code its working fine with out any block OR you simply implement Player1SideCallback by MyRecyclerViewAdapter class and override its method at class . level
– Chetan Joshi
Nov 22 '18 at 6:39
add a comment |
So in my app I have 2 players in a game. Player 1 is assigned either heads or tails, and then player 2 is to be assigned the side that player 1 is not. i.e. if player 1 is heads, then player 2 is tails, and vice versa.
To check to see which side player 1 is before assigning a side to player 2, I have a callback setup to get which side player 1 has been assigned. And the callback looks like this.
public interface Player1SideCallback {
void onCallback(String side);
}
private void player1Side(final Player1SideCallback callback) {
FCGames.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
String player1Side = dataSnapshot.child(currentGameID).child("HeadsOrTails").getValue().toString();
callback.onCallback(player1Side);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
It's a pretty basic callback.
Now, I am when a player clicks the join button they are assigned to the game as player 2 using this snippet:
adapter = new MyRecyclerViewAdapter(this, openGames, new MyRecyclerViewAdapter.OnButtonClickListener() {
@Override public void onButtonClick(String id, final int position) {
String gameID = openGames.get(position);
second = new User(userName, 0, getSide2(), FirebaseAuth.getInstance().getUid(), gameID, false);
gm.setUser2(second);
FCGames.child(gameID).child("user2").setValue(second);
HomePage.setCurrentGame(gameID);
HomePage.setUserPosition(2);
}});
enter code here
As you can see, I have left line spaces around the variable second because that is where the new player is actually being created. And where it says getSide2() it is assigning a side to the player 2.
But, I am struggling to get this right. I call my callback in a method called getSide2() to try to properly assign the right side to user 2
private String getSide2() {
player1Side(new Player1SideCallback() {
@Override
public void onCallback(String side) {
if(side.equals("Heads")) {
String side2 = "Tails";
} else {
String side2 = "Heads";
}
}
});
}
So I want to assign the player2 the side with the variable side2 but I am not sure how to return this value from a method using a callback.
How can I use this type of logic to properly assign the correct side to user 2?
java
So in my app I have 2 players in a game. Player 1 is assigned either heads or tails, and then player 2 is to be assigned the side that player 1 is not. i.e. if player 1 is heads, then player 2 is tails, and vice versa.
To check to see which side player 1 is before assigning a side to player 2, I have a callback setup to get which side player 1 has been assigned. And the callback looks like this.
public interface Player1SideCallback {
void onCallback(String side);
}
private void player1Side(final Player1SideCallback callback) {
FCGames.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
String player1Side = dataSnapshot.child(currentGameID).child("HeadsOrTails").getValue().toString();
callback.onCallback(player1Side);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
It's a pretty basic callback.
Now, I am when a player clicks the join button they are assigned to the game as player 2 using this snippet:
adapter = new MyRecyclerViewAdapter(this, openGames, new MyRecyclerViewAdapter.OnButtonClickListener() {
@Override public void onButtonClick(String id, final int position) {
String gameID = openGames.get(position);
second = new User(userName, 0, getSide2(), FirebaseAuth.getInstance().getUid(), gameID, false);
gm.setUser2(second);
FCGames.child(gameID).child("user2").setValue(second);
HomePage.setCurrentGame(gameID);
HomePage.setUserPosition(2);
}});
enter code here
As you can see, I have left line spaces around the variable second because that is where the new player is actually being created. And where it says getSide2() it is assigning a side to the player 2.
But, I am struggling to get this right. I call my callback in a method called getSide2() to try to properly assign the right side to user 2
private String getSide2() {
player1Side(new Player1SideCallback() {
@Override
public void onCallback(String side) {
if(side.equals("Heads")) {
String side2 = "Tails";
} else {
String side2 = "Heads";
}
}
});
}
So I want to assign the player2 the side with the variable side2 but I am not sure how to return this value from a method using a callback.
How can I use this type of logic to properly assign the correct side to user 2?
java
java
asked Nov 22 '18 at 6:05
CoderKliptoCoderKlipto
617
617
why you have putted callback code to method its should be global or implemented by class level.
– Chetan Joshi
Nov 22 '18 at 6:16
@ChetanJoshi because I only want the value to be calledback whenever I need it to be (in this case when I call the method). How can I use the callback differently so that I can just get the value from the callback when I need it?
– CoderKlipto
Nov 22 '18 at 6:18
its not working as you think , because whenever callback comes method will not be triggering it self. And as you want callback only be called when onSuccess() called from your first code.
– Chetan Joshi
Nov 22 '18 at 6:24
@ChetanJoshi Okay so say I place the callback right above myadapterin the onCreate() method. How would I pull the valueside2out and then use it in myadapterto make user2?
– CoderKlipto
Nov 22 '18 at 6:27
Not in OnCreate() , its no need to add callback code under any block of code its working fine with out any block OR you simply implement Player1SideCallback by MyRecyclerViewAdapter class and override its method at class . level
– Chetan Joshi
Nov 22 '18 at 6:39
add a comment |
why you have putted callback code to method its should be global or implemented by class level.
– Chetan Joshi
Nov 22 '18 at 6:16
@ChetanJoshi because I only want the value to be calledback whenever I need it to be (in this case when I call the method). How can I use the callback differently so that I can just get the value from the callback when I need it?
– CoderKlipto
Nov 22 '18 at 6:18
its not working as you think , because whenever callback comes method will not be triggering it self. And as you want callback only be called when onSuccess() called from your first code.
– Chetan Joshi
Nov 22 '18 at 6:24
@ChetanJoshi Okay so say I place the callback right above myadapterin the onCreate() method. How would I pull the valueside2out and then use it in myadapterto make user2?
– CoderKlipto
Nov 22 '18 at 6:27
Not in OnCreate() , its no need to add callback code under any block of code its working fine with out any block OR you simply implement Player1SideCallback by MyRecyclerViewAdapter class and override its method at class . level
– Chetan Joshi
Nov 22 '18 at 6:39
why you have putted callback code to method its should be global or implemented by class level.
– Chetan Joshi
Nov 22 '18 at 6:16
why you have putted callback code to method its should be global or implemented by class level.
– Chetan Joshi
Nov 22 '18 at 6:16
@ChetanJoshi because I only want the value to be calledback whenever I need it to be (in this case when I call the method). How can I use the callback differently so that I can just get the value from the callback when I need it?
– CoderKlipto
Nov 22 '18 at 6:18
@ChetanJoshi because I only want the value to be calledback whenever I need it to be (in this case when I call the method). How can I use the callback differently so that I can just get the value from the callback when I need it?
– CoderKlipto
Nov 22 '18 at 6:18
its not working as you think , because whenever callback comes method will not be triggering it self. And as you want callback only be called when onSuccess() called from your first code.
– Chetan Joshi
Nov 22 '18 at 6:24
its not working as you think , because whenever callback comes method will not be triggering it self. And as you want callback only be called when onSuccess() called from your first code.
– Chetan Joshi
Nov 22 '18 at 6:24
@ChetanJoshi Okay so say I place the callback right above my
adapter in the onCreate() method. How would I pull the value side2 out and then use it in my adapter to make user2?– CoderKlipto
Nov 22 '18 at 6:27
@ChetanJoshi Okay so say I place the callback right above my
adapter in the onCreate() method. How would I pull the value side2 out and then use it in my adapter to make user2?– CoderKlipto
Nov 22 '18 at 6:27
Not in OnCreate() , its no need to add callback code under any block of code its working fine with out any block OR you simply implement Player1SideCallback by MyRecyclerViewAdapter class and override its method at class . level
– Chetan Joshi
Nov 22 '18 at 6:39
Not in OnCreate() , its no need to add callback code under any block of code its working fine with out any block OR you simply implement Player1SideCallback by MyRecyclerViewAdapter class and override its method at class . level
– Chetan Joshi
Nov 22 '18 at 6:39
add a comment |
0
active
oldest
votes
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%2f53424791%2freturning-value-from-callback-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53424791%2freturning-value-from-callback-in-java%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
why you have putted callback code to method its should be global or implemented by class level.
– Chetan Joshi
Nov 22 '18 at 6:16
@ChetanJoshi because I only want the value to be calledback whenever I need it to be (in this case when I call the method). How can I use the callback differently so that I can just get the value from the callback when I need it?
– CoderKlipto
Nov 22 '18 at 6:18
its not working as you think , because whenever callback comes method will not be triggering it self. And as you want callback only be called when onSuccess() called from your first code.
– Chetan Joshi
Nov 22 '18 at 6:24
@ChetanJoshi Okay so say I place the callback right above my
adapterin the onCreate() method. How would I pull the valueside2out and then use it in myadapterto make user2?– CoderKlipto
Nov 22 '18 at 6:27
Not in OnCreate() , its no need to add callback code under any block of code its working fine with out any block OR you simply implement Player1SideCallback by MyRecyclerViewAdapter class and override its method at class . level
– Chetan Joshi
Nov 22 '18 at 6:39