Values of override method not reflecting on main variables











up vote
0
down vote

favorite












I am trying to change values of my class from an override method as shown below:



 public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");

}
return;
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}

}
}
return;
}
}


From the analyse() method i am calling the getEntity() method, the override method is working fine and is changing the value of identified and entityIdentified accordingly but, when the method is completed the updates are not reflected on the variables.



I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?



I have tried implementing threads as well, to make sure the method getEntity() gets completed before returning to the main, but the problem persists.










share|improve this question
























  • Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
    – Ashutoshg
    Nov 10 at 10:52












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    Nov 10 at 11:21










  • You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
    – Cool Guy CG
    Nov 10 at 15:48










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    Nov 11 at 7:48















up vote
0
down vote

favorite












I am trying to change values of my class from an override method as shown below:



 public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");

}
return;
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}

}
}
return;
}
}


From the analyse() method i am calling the getEntity() method, the override method is working fine and is changing the value of identified and entityIdentified accordingly but, when the method is completed the updates are not reflected on the variables.



I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?



I have tried implementing threads as well, to make sure the method getEntity() gets completed before returning to the main, but the problem persists.










share|improve this question
























  • Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
    – Ashutoshg
    Nov 10 at 10:52












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    Nov 10 at 11:21










  • You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
    – Cool Guy CG
    Nov 10 at 15:48










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    Nov 11 at 7:48













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to change values of my class from an override method as shown below:



 public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");

}
return;
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}

}
}
return;
}
}


From the analyse() method i am calling the getEntity() method, the override method is working fine and is changing the value of identified and entityIdentified accordingly but, when the method is completed the updates are not reflected on the variables.



I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?



I have tried implementing threads as well, to make sure the method getEntity() gets completed before returning to the main, but the problem persists.










share|improve this question















I am trying to change values of my class from an override method as shown below:



 public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
} else {
mimicOtherMessage("Sorry, I have failed to understand your question");

}
return;
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified="basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified="basic";
}
}
}

}
}
return;
}
}


From the analyse() method i am calling the getEntity() method, the override method is working fine and is changing the value of identified and entityIdentified accordingly but, when the method is completed the updates are not reflected on the variables.



I have tried to debug, i could see the values changing in the override method, but when i check the values of the variables after the override method, i could only get the initial values. Somebody can help me on this please?



I have tried implementing threads as well, to make sure the method getEntity() gets completed before returning to the main, but the problem persists.







java android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:35









Frank van Puffelen

224k26365392




224k26365392










asked Nov 10 at 10:27









Samiirah Aujub

45




45












  • Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
    – Ashutoshg
    Nov 10 at 10:52












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    Nov 10 at 11:21










  • You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
    – Cool Guy CG
    Nov 10 at 15:48










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    Nov 11 at 7:48


















  • Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
    – Ashutoshg
    Nov 10 at 10:52












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    Nov 10 at 11:21










  • You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
    – Cool Guy CG
    Nov 10 at 15:48










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    Nov 11 at 7:48
















Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
Nov 10 at 10:52






Need more clarification on your question. entityIdentified = entity.getKey(); //changed here but not after this method completes identified = true; //changed here but not after this method completes In override method OnDataChange() ?
– Ashutoshg
Nov 10 at 10:52














when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
Nov 10 at 11:21




when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
Nov 10 at 11:21












You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
Nov 10 at 15:48




You are doing the changes with the assumption that your variables will be assigned instantly, from top to down, however, you should remember that the network operation with Firebase is running concurrently on a separate thread which might finish later. So you are seeing the variables being set, but they are set after the Firebase operation is done, which is independent of you code flow.
– Cool Guy CG
Nov 10 at 15:48












Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
Nov 11 at 7:48




Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
Nov 11 at 7:48












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:



public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}

}
}
}
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
// the logic here will have to also be moved into the Firebase call
}
}


Note that, the parts that required the check on identified have all been moved to the onDataChanged method.






share|improve this answer





















  • Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
    – Samiirah Aujub
    Nov 11 at 7:50










  • That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
    – Cool Guy CG
    Nov 11 at 19:07










  • Okay thank you. It works now
    – Samiirah Aujub
    Nov 12 at 5:41


















up vote
0
down vote













Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.






share|improve this answer





















  • Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
    – Samiirah Aujub
    Nov 11 at 16:21











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238024%2fvalues-of-override-method-not-reflecting-on-main-variables%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








up vote
0
down vote



accepted










The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:



public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}

}
}
}
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
// the logic here will have to also be moved into the Firebase call
}
}


Note that, the parts that required the check on identified have all been moved to the onDataChanged method.






share|improve this answer





















  • Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
    – Samiirah Aujub
    Nov 11 at 7:50










  • That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
    – Cool Guy CG
    Nov 11 at 19:07










  • Okay thank you. It works now
    – Samiirah Aujub
    Nov 12 at 5:41















up vote
0
down vote



accepted










The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:



public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}

}
}
}
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
// the logic here will have to also be moved into the Firebase call
}
}


Note that, the parts that required the check on identified have all been moved to the onDataChanged method.






share|improve this answer





















  • Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
    – Samiirah Aujub
    Nov 11 at 7:50










  • That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
    – Cool Guy CG
    Nov 11 at 19:07










  • Okay thank you. It works now
    – Samiirah Aujub
    Nov 12 at 5:41













up vote
0
down vote



accepted







up vote
0
down vote



accepted






The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:



public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}

}
}
}
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
// the logic here will have to also be moved into the Firebase call
}
}


Note that, the parts that required the check on identified have all been moved to the onDataChanged method.






share|improve this answer












The code you have written is not very detailed to me, but your logic could be restructured in a way, similar to what I have done below:



public class QuestionAnalyser extends AppCompatActivity {
String question;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);

}

public void analyse(String userQues) {
if (!(Interface.deviceLang.equals("en"))) {
TranslatorServant t = new TranslatorServant();
question = t.translateText(userQues);
} else {
question = userQues;
}
question = question.replaceAll("\p{Punct}|\d", "");
final String words = question.split(" ");
getEntity(words);
// remove any logic that depends on the return of getEntity(words) from here
// it is best if the logic is called from within the Firebase call
}

private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
mimicOtherMessage("found");
entityIdentified = entity.getKey();
identified = true;// under what condition will this be false
// this is the best place to put the logic
initialiseEntityServant(entityIdentified, question, words);
} else {
identified = false;// I hope this condition for setting it to false is satisfactory
mimicOtherMessage("Sorry, I have failed to understand your question");
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
}
}
}

}
}
}
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

}
});
// the logic here will have to also be moved into the Firebase call
}
}


Note that, the parts that required the check on identified have all been moved to the onDataChanged method.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 16:11









Cool Guy CG

69548




69548












  • Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
    – Samiirah Aujub
    Nov 11 at 7:50










  • That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
    – Cool Guy CG
    Nov 11 at 19:07










  • Okay thank you. It works now
    – Samiirah Aujub
    Nov 12 at 5:41


















  • Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
    – Samiirah Aujub
    Nov 11 at 7:50










  • That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
    – Cool Guy CG
    Nov 11 at 19:07










  • Okay thank you. It works now
    – Samiirah Aujub
    Nov 12 at 5:41
















Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
Nov 11 at 7:50




Yes, i get your point. But what if i need the on change method to be completed first before i proceed? Is there a way to put the firebase method execution in a thread and ensure the thread is executed first then resumes with the main?
– Samiirah Aujub
Nov 11 at 7:50












That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
– Cool Guy CG
Nov 11 at 19:07




That will cause your application to hang, it is a form of NetworkOnMainThread. You should never tie your application's main thread to a network operation, they should be asynchronous. However, you can move almost all your operations into the onDataChanged method, where all your views can be updated with a runOnUIThread call.
– Cool Guy CG
Nov 11 at 19:07












Okay thank you. It works now
– Samiirah Aujub
Nov 12 at 5:41




Okay thank you. It works now
– Samiirah Aujub
Nov 12 at 5:41












up vote
0
down vote













Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.






share|improve this answer





















  • Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
    – Samiirah Aujub
    Nov 11 at 16:21















up vote
0
down vote













Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.






share|improve this answer





















  • Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
    – Samiirah Aujub
    Nov 11 at 16:21













up vote
0
down vote










up vote
0
down vote









Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.






share|improve this answer












Please move your code written after getEntity()
Inside onDataChange() method. When onDataChange() completes it will execute added code. You will get latest values in variables.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 12:28









Ashutoshg

576




576












  • Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
    – Samiirah Aujub
    Nov 11 at 16:21


















  • Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
    – Samiirah Aujub
    Nov 11 at 16:21
















Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
Nov 11 at 16:21




Thank you for your answer, like i mentioned above is there anyway using threads to make sure the onChange() method gets completed before the main thread resumes?
– Samiirah Aujub
Nov 11 at 16:21


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238024%2fvalues-of-override-method-not-reflecting-on-main-variables%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff