Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么同一个字符串有多个结果,为什么它们不同_Android_Stringbuilder - Fatal编程技术网

Android 为什么同一个字符串有多个结果,为什么它们不同

Android 为什么同一个字符串有多个结果,为什么它们不同,android,stringbuilder,Android,Stringbuilder,我在SD卡目录中循环,读取每个文件中的文本,并将文本写入动态添加的TextView。 每个文件都包含换行符,我认为这是问题所在。 我已经搜索了SO,Google尝试了一些建议,现在我的代码返回并打印每个文本文件两次。第一个只包含文本,直到第一个换行符,第二个按我需要的方式打印文本。 示例文本文件test.txt This is a test. And I cannot make it work 所需输出为 test.txt This is a test.

我在SD卡目录中循环,读取每个文件中的文本,并将文本写入动态添加的TextView。 每个文件都包含换行符,我认为这是问题所在。 我已经搜索了SO,Google尝试了一些建议,现在我的代码返回并打印每个文本文件两次。第一个只包含文本,直到第一个换行符,第二个按我需要的方式打印文本。 示例文本文件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);

此代码有什么问题?

您的代码应该是这样的

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();
    }
}

message.setText(文本)