2013-08-27 5 views
0

SDCard 디렉토리를 반복하면서 각 파일의 텍스트를 읽고 동적으로 추가 된 textViews에 텍스트를 쓰는 중입니다. 각 파일에는 줄 바꿈이 들어 있습니다. 문제가있는 곳마다 생각합니다. 나는 SO와 Google을 검색하여 몇 가지 제안을 시도했지만 이제는 내 코드가 반환되어 각 텍스트 파일을 두 번 인쇄합니다. 첫 번째 줄 바꿈 때까지 첫 번째 텍스트 만 들어, ssecond 정확히 내가 원하는 방식으로 텍스트를 인쇄합니다. 예는 텍스트 파일 TEST.TXT동일한 문자열에 대해 여러 개의 결과가 나타나는 이유는 무엇입니까?

This is a test. 
    And I cannot make it work 

원하는 출력은 I 원하는 출력을 얻을

test.txt 
    This is a test. 
    And I cannot make it work 

보기 제가

test.txt 
    This is a test. 

번째 시간을받는 추가 처음 . 그것은 모든 TXT 파일을 여기에

내 코드

String sdcard = Environment.getExternalStorageDirectory() + "/.BELIEVE/PushMessages/"; 

    // go to your directory 
    File fileList = new File(sdcard); 

    //check if dir is not null 
    if (fileList != null){ 

     // so we can list all files 
     File[] filenames = fileList.listFiles(); 

     // loop through each file 
     for (File tmpf : filenames){ 

     StringBuilder text = new StringBuilder(); 

      try { 
      BufferedReader br = new BufferedReader(new FileReader(tmpf)); 
      String name = tmpf.getName(); 
      String line; 

      while ((line = br.readLine()) != null) { 
      text.append(line); 
      text.append('\n'); 
      TextView title = new TextView(PushMessagesPage.this); 
      TextView message = new TextView(PushMessagesPage.this); 
      ll.addView(title); 
       title.setLayoutParams(textViewParams); 
      title.setTextAppearance(this, android.R.attr.textAppearanceLarge); 
      title.setTextColor(0xff33b5e5); 
      title.setText(name); 
      ll.addView(message);  
          message.setLayoutParams(textViewParams); 
      message.setTextColor(0xffffffff); 
      message.setText(text); 

이 코드에 어떤 문제가되어 함께이 무엇입니까?

+1

'message.setText (text);'는 while 루프에서 벗어나야합니다. –

+0

문제가 해결 되었습니까? –

+0

Sunil, 지난 밤에 내가 당신의 의견을 읽은 후에 나는 위의 코드를 검사했고 당신이 절대적으로 옳았다는 것이 분명 해졌다. 조언 주셔서 감사합니다, 나는 당신의 의견을 upvoted !! – jb15613

답변

1

코드는 다음과 같아야합니다.

for (File tmpf : filenames) { 
    StringBuilder text = new StringBuilder(); 
    BufferedReader br = null; 
    try { 
     br = new BufferedReader(new FileReader(tmpf)); 
     String name = tmpf.getName(); 
     String line; 

     TextView title = new TextView(StackDemosActivity.this); 
     ll.addView(title); 
     title.setLayoutParams(textViewParams); 
     title.setTextAppearance(this, 
       android.R.attr.textAppearanceLarge); 
     title.setTextColor(0xff33b5e5); 
     title.setText(name); 

     TextView message = new TextView(StackDemosActivity.this); 
     ll.addView(message); 
     message.setLayoutParams(textViewParams); 
     message.setTextColor(0xffffffff); 

     while ((line = br.readLine()) != null) { 
      text.append(line); 
      text.append('\n'); 
     } 
     message.setText(text); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+1

예,이 답변은 100 % 근무 중입니다. 내가 갇히게되면, 나는 많은 다른 것들을 시도해 내 코드가 모두 엉키게된다. 이것은 훌륭하고 깨끗하고 훌륭한 답변입니다! – jb15613

+0

setTextAppearence (this, android.R.attr.textAppearenceLarge)는 API 13 이하에서만 작동한다는 점에 유의해야합니다. 14 이상을 사용하는 경우 setTextAppearence (this, android.R.style.TextAppearence_Large)이어야합니다. – jb15613

관련 문제