简体   繁体   中英

How to clear result in string array Android/Java

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.

Extract the checking functionality to a method like this:

ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        checkAmbiguousWord();
    }   
});  

...

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    Integer ambiguousIndex = findAmbiguousWordIndex(textToCheck);
    view.setText(ambiguousIndex != null ? meanings[ambiguousIndex] : "No ambiguous word/s found.");
}

/**
 * @param text checked for ambguous words
 * @return the index of the ambiguous word in the {@code words} array or
 * null if no ambiguous word is found
 */
private Integer findAmbiguousWordIndex(String text) {
    final String lowerCasedText = text.toLowerCase();
    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            return i;
        }
    }
    return null;
}

This eliminates the dependence on a hidden internal state (the check variable). This programming style allows you to separate the view controller code and the business functionality and eventually write tests for the business functionality independently from the views.

UPDATE: to show multiple ambiguous words, use a list for the indexes

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    List<Integer> ambiguousIndexes = findAmbiguousWordIndexes(textToCheck);
    view.setText(!ambiguousIndexes.isEmpty() ? ambigousIndexesToMessage(ambiguousIndexes) : "No ambiguous word/s found.");
}

    public String ambigousIndexesToMessage(List<Integer> ambiguousIndexes) {
        // create the text using the indexes
        // this is an example implementation
        return ambiguousIndexes.toString(); // creates a list of "1","2",...
    }

/**
 * @param text checked for ambguous words
 * @return the list of indexes of the the ambiguous words in the {@code words} array          
 */
private List<Integer> findAmbiguousWordIndexes(String text) {
    final String lowerCasedText = text.toLowerCase();
    final List<Integer> ambiguousWordIndexList = new ArrayList<Integer>();
    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            ambiguousWordIndexList.add(i);
        }
    }
    return ambiguousWordIndexList;
}

您需要在方法开始时将检查设置为false。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM