2013-09-28 106 views
0

我有一個應用程序,它從用戶獲取數據,然後顯示來自數據庫的結果。從該數據庫中,我將數據推送到ArrayList。例如,用戶輸入句子(例如「Hello world」),應用程序將循環來自數據庫的記錄,並且當它找到某事時,它將向用戶顯示它找到一個單詞。像這樣:ArrayList添加結果加倍

歧義詞:世界 含義:世界的意義。

這裏是我工作的代碼:

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的新手,所以我需要你的幫助來找到那個錯誤的代碼行。任何幫助深表感謝。提前致謝。

+0

你刪除表? – robotoaster

+0

不,我不會丟掉任何表格 – Dunkey

回答

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

那麼如何清除數組列表? – Dunkey

+0

你可以通過調用ArrayList類的clear()方法來實現;看到我編輯的答案和這個類的文檔[ArrayList](https://developer.android.com/reference/java/util/ArrayList.html) – Eudhan

+0

但我想知道爲什麼我的循環從不捕獲第一個記錄? – Dunkey

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

每次執行onClick時,都會將字符串添加到ArrayList中。在for循環之前清除它們。每個陣列的方法爲.clear()

+0

感謝兄弟我知道了。 – Dunkey