我有一個應用程序,它從用戶獲取數據,然後顯示來自數據庫的結果。從該數據庫中,我將數據推送到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的新手,所以我需要你的幫助來找到那個錯誤的代碼行。任何幫助深表感謝。提前致謝。
你刪除表? – robotoaster
不,我不會丟掉任何表格 – Dunkey