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);
이 코드에 어떤 문제가되어 함께이 무엇입니까?
'message.setText (text);'는 while 루프에서 벗어나야합니다. –
문제가 해결 되었습니까? –
Sunil, 지난 밤에 내가 당신의 의견을 읽은 후에 나는 위의 코드를 검사했고 당신이 절대적으로 옳았다는 것이 분명 해졌다. 조언 주셔서 감사합니다, 나는 당신의 의견을 upvoted !! – jb15613