Android arraylist添加的结果增加了一倍

Android arraylist添加的结果增加了一倍,android,arraylist,android-sqlite,Android,Arraylist,Android Sqlite,我有一个应用程序,它从用户那里获取数据,然后显示数据库的结果。从该数据库中,我将数据推送到ArrayList。例如,用户输入一个句子(例如“Hello world”),应用程序将循环数据库中的记录,当它找到某个单词时,它将向用户显示它找到了一个单词。像这样: 模棱两可的词:世界 意义:世界的意义。 以下是我正在编写的代码: private DBHelper dbHelper; Cursor cursor; ArrayList<String> colWords = n

我有一个应用程序,它从用户那里获取数据,然后显示数据库的结果。从该数据库中,我将数据推送到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(); 
}
private-DBHelper-DBHelper;
光标;
ArrayList colWords=新的ArrayList();
ArrayList colmeansions=新的ArrayList();
字符串[]个单词;
字符串[]含义;
ok=(按钮)findViewById(R.id.button1);
ok.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
//检查歧义词();
dbHelper=新的dbHelper(MainActivity.this);
试一试{
dbHelper.createDataBase();
}捕获(ioe异常ioe){
抛出新错误(“无法创建数据库”);
}
试一试{
dbHelper.openDataBase();
}捕获(SQLException sqle){
抛出sqle;
}
cursor=dbHelper.getAllWords();
对于(cursor.moveToFirst();cursor.moveToNext();cursor.isAfterLast()){
添加(cursor.getString(1));
colmeansions.add(cursor.getString(2));
}
检查歧义词();
}   
});
}
私有void checkAmbiguousWord(){
最终字符串textToCheck=text.getText().toString();
列出含糊不清的索引=findambiguouswordindex(textToCheck);
view.setText(!ambiguousIndexes.isEmpty()?
歧义索引消息(歧义索引):“未找到歧义词。”);
}
/**
*@param text已检查是否存在歧义词
*@返回{@code words}数组中歧义词的索引列表
*/
私有列表findambiguouswordindex(字符串文本){
最后一个字符串lowerCasedText=text.toLowerCase();
最终列表含糊不清WordIndexList=new ArrayList();
words=(字符串[])colWords.toArray(新字符串[colWords.size()]);
语义=(字符串[])colmeansions.toArray(新字符串[colmeans.size()]);
for(int i=0;i
但我的问题是,每次我输入相同的Hello World,结果都会被添加

模棱两可的词:世界 意义:世界的意义 模棱两可的词:世界 意义:世界的意义

我的代码中有什么错误?我是Java新手,所以我需要您的帮助来查找错误的代码行。非常感谢您的帮助。提前谢谢

for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
                colWords.add(cursor.getString(1));
                colMeanings.add(cursor.getString(2));
            }
每次执行onClick时,都会将字符串添加到ArrayList中。在for循环之前清除它们
.clear()
每个数组的方法。

每次调用后(单击“确定”按钮),您不会清除数组列表中的colWords和colmeansions:

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

您是否要在下次运行之前删除表?不,我不会删除任何表您可以通过调用ArrayList类的clear()方法来做到这一点;请参阅我编辑的答案和本课程的文档。您是否检查了第一条记录的语法?我在不同的循环(光标循环和“包含”循环)中没有看到任何问题。是的,我检查了,但仍然没有显示第一条记录?