我正在尝试通过覆盖方法更改类的值,如下所示:
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;
}
}
在analyse()
方法中,我正在调用getEntity()
方法,重写方法可以正常工作,并且正在相应地更改identified
和entityIdentified
的值,但是,当方法完成后,更新不会反映在变量上。
我尝试调试,我可以看到覆盖方法中的值发生了变化,但是当我在覆盖方法之后检查变量的值时,我只能得到初始值。有人可以帮我吗?
我也尝试实现线程,以确保方法getEntity()
在返回主方法之前完成,但是问题仍然存在。
答案 0 :(得分:0)
您编写的代码对我来说不是很详细,但是您的逻辑可以通过某种方式进行重组,类似于我在下面所做的事情:
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
}
}
请注意,需要对identified
进行检查的部分已全部移至onDataChanged
方法。
答案 1 :(得分:0)
请移动您在getEntity()之后编写的代码 在onDataChange()方法内部。当onDataChange()完成时,它将执行添加的代码。您将获得变量的最新值。