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









New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















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












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    yesterday










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










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    21 hours ago















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









New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















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












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    yesterday










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










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    21 hours ago













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









New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Frank van Puffelen

217k25361384




217k25361384






New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Samiirah Aujub

13




13




New contributor




Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Samiirah Aujub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    yesterday










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










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    21 hours ago


















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












  • when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
    – Samiirah Aujub
    yesterday










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










  • Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
    – Samiirah Aujub
    21 hours ago
















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
yesterday






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
yesterday














when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday




when i am checking the values of the global variables after the override method OnDataChange(), the updates are not reflecting.
– Samiirah Aujub
yesterday












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
yesterday




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
yesterday












Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago




Thank you for your answer @CoolGuyCG . I was suspecting something like this was happening but was not sure.
– Samiirah Aujub
21 hours ago












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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
    21 hours ago










  • 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
    10 hours ago


















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
    12 hours ago











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


}
});






Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.










 

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
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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
    21 hours ago










  • 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
    10 hours ago















up vote
0
down vote













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
    21 hours ago










  • 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
    10 hours ago













up vote
0
down vote










up vote
0
down vote









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 yesterday









Cool Guy CG

68048




68048












  • 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
    21 hours ago










  • 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
    10 hours ago


















  • 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
    21 hours ago










  • 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
    10 hours ago
















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
21 hours ago




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
21 hours ago












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
10 hours ago




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
10 hours ago












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
    12 hours ago















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
    12 hours ago













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 16 hours ago









Ashutoshg

555




555












  • 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
    12 hours ago


















  • 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
    12 hours ago
















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
12 hours ago




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
12 hours ago










Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.













Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.












Samiirah Aujub is a new contributor. Be nice, and check out our Code of Conduct.















 


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




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues