Im循环访问SDCard目录,读取每个文件中的文本以及将文本写入动态添加的textViews。每个文件都包含换行符,我认为它是问题所在。 我搜索过,和谷歌,尝试了一些建议,现在我的代码返回并打印每个文本文件两次。第一个只包含文本,直到第一个换行符为止,第二个文本打印文本完全是我需要的。 实施例文本文件test.txt为什么我会得到同一个字符串的多个结果,以及它们为什么不同
This is a test.
And I cannot make it work
所需的输出是
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循环之外 –
你的问题解决了吗? –
苏尼尔,昨天晚上我读了你的评论后,我检查了我的上面的代码,很明显你是完全正确的。感谢您的建议,我upvoted您的评论! – jb15613