2013-09-28 3 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(); 
} 

하지만 내 문제는 결과가 추가됩니다 모든 시간 I 입력 같은 안녕하세요입니다.

모호한 단어 : 세계 의미 : 세계 모호한 단어의 의미 : 세계 의미 : 세계

잘못 내 코드에서 무엇의 의미는? 나는 Java에 익숙하지 않으므로 잘못된 코드 줄을 찾는 데 도움이 필요합니다. 어떤 도움이라도 대단히 감사합니다. 미리 감사드립니다.

+0

이 떨어지고 테이블 : (당신의 버튼을 "확인"을 클릭 한 번으로) 각 호출 후 – robotoaster

+0

아니요, 어떤 테이블도 놓치지 않고 있습니다. – Dunkey

답변

1

, 당신은 colWords 및 colMeanings에 대한 ArrayList의 선택을 취소하지?
+0

그래서 arraylist를 어떻게 지울 수 있습니까? – Dunkey

+0

ArrayList 클래스의 clear() 메서드를 호출하여이 작업을 수행 할 수 있습니다. 내 편집 된 답변 및이 클래스의 설명서를 참조하십시오. [ArrayList] – 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() 각 배열에 대한 방법. 또 다른 실행 전에

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