0

我有一个应用程序,它从用户那里获取数据,然后从数据库中显示结果。从该数据库中,我将数据推送到 ArrayList。例如,用户输入一个句子(例如“Hello world”),应用程序将循环访问数据库中的记录,当它找到某些东西时,它会向用户显示它找到了一个单词。像这样:

歧义词:世界意义:世界的意义。

这是我正在处理的代码:

    private DBHelper dbHelper;
Cursor cursor;

    ArrayList<String> colWords = new ArrayList<String>();
ArrayList<String> colMeanings = new ArrayList<String>();
String[] words;
String[] meanings;

    ok = (Button) findViewById (R.id.button1);
    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //checkAmbiguousWord();
            dbHelper = new DBHelper(MainActivity.this);

            try {

                    dbHelper.createDataBase();

            } catch (IOException ioe) {

                    throw new Error("Unable to create database");

            }

            try {

                    dbHelper.openDataBase();

            } catch (SQLException sqle) {

                    throw sqle;

            }

            cursor = dbHelper.getAllWords();

            for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
                colWords.add(cursor.getString(1));
                colMeanings.add(cursor.getString(2));
            }

            checkAmbiguousWord();
        }   
    });
}

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

/**
 * @param text checked for ambiguous words
 * @return the list of indexes of 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>();

    words = (String[]) colWords.toArray(new String[colWords.size()]);
    meanings = (String[]) colMeanings.toArray(new String[colMeanings.size()]);

    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            ambiguousWordIndexList.add(i);
        }
    }
    return ambiguousWordIndexList;
} 

public String ambigousIndexesToMessage(List<Integer> ambiguousIndexes) {
    // create the text using the indexes
    // this is an example implementation
    StringBuilder sb = new StringBuilder();

    for (Integer index : ambiguousIndexes) {
        sb.append("Ambiguous words: ");
        sb.append(words[index] + "\nMeaning: " + meanings[index] + "\n");
        sb.append(" ");
    }
    return sb.toString(); 
}

但我的问题是,每次我输入相同的 Hello World 时,都会添加结果。

歧义词:世界 意义:世界的意义 歧义词:世界 意义:世界的意义

我的代码有什么问题?我是 Java 新手,所以我需要你的帮助来找到那行错误的代码。任何帮助深表感谢。提前致谢。

4

2 回答 2

1
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
                colWords.add(cursor.getString(1));
                colMeanings.add(cursor.getString(2));
            }

每次执行 onClick 时,都将字符串添加到 ArrayList 中。在 for 循环之前清除它们。.clear()每个数组的方法。

于 2013-09-28T09:25:19.840 回答
1

每次通话后(单击“确定”按钮),您不会清除 colWords 和 colMeanings 的 ArrayList :

cursor = dbHelper.getAllWords();

colWords.clear();///added code
colMeanings.clear();///added code
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
 colWords.add(cursor.getString(1));
 colMeanings.add(cursor.getString(2));
}

checkAmbiguousWord();
于 2013-09-28T09:28:05.400 回答