arraylist添加结果加倍

时间:2013-09-28 09:17:25

标签: android arraylist android-sqlite

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

暧昧的词:世界 含义:世界的意义。

以下是我正在处理的代码:

    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的新手,所以我需要你的帮助才能找到错误的代码行。任何帮助深表感谢。提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

每次调用后(点击你的按钮“ok”),你不会清除colList和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();