2013-09-28 76 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