After finding a word in a sentence, I want to the user to be able to clear his entries and type again a sentence in the edittext.
This is what I have tried so far:
final String[] words = {"cowboy", "animal"};
final String[] meanings = { "meaning1", "meaning2" };
Boolean check = false;
private void initControls() {
// TODO Auto-generated method stub
text = (EditText) findViewById (R.id.editText1);
view = (TextView) findViewById (R.id.textView1);
clear = (Button) findViewById (R.id.button2);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setText("");
view.setText("");
}
});
ok = (Button) findViewById (R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
findAmbiguousWord();
}
});
}
private void findAmbiguousWord(){
String string = text.getText().toString();
int index = 0;
for (int i = 0; i < words.length; i++) {
if (string.toLowerCase().contains(words[i].toLowerCase())) {
check = true;
index = i;
}
}
view.setText(check ? meanings[index] : "No ambiguous word/s found.");
}
When I tried to clear my entry and typed again new entry, the same result was displaying. what should be done to avoid displaying the previous result? Any help is much appreciated.
TextViewis not cleared?int index = -1;or haveview.setText(check ? meanings[index] : "No ambiguous word/s found."); }inside theifcondition. Also have anelsestatement there. I am saying this as it might be that you never enter theifcondition when there is no substring of that type and then the result always is the word atmeanings[0]as index is 0. Try that.