TextView settext 显示单行而不是全部

TextView settext showing single line instead of all

上下文: textview应该显示文件中所有保存的数据,以行

的形式

问题:只显示当前数据而不是以前的所有记录。

FileInputStream fin =  new   FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/courtrecord.txt");
          DataInputStream din  =    new DataInputStream(fin);
         String   fromfile=din.readLine();
         textview.setText(fromfile);

         while(( fromfile  =  din.readLine())!=null)   
         {
           String  teamAName  = fromfile.substring(0,fromfile.indexOf('@'));
           String teamAScore = fromfile.substring(fromfile.indexOf('@')+1,fromfile.indexOf('#'));
           String teamBName = fromfile.substring(fromfile.indexOf('#')+1,fromfile.indexOf('$'));
           String teamBScore = fromfile.substring(fromfile.indexOf('$')+1,fromfile.indexOf('%'));
           // 0-@,  @-#, #-$, $-%
           textview.setText(" "+ teamAName.toString() +" "+ teamAScore.toString() + " "+ teamBName.toString()+ " "+teamBScore.toString()+ "\n");
         }
    }
    catch(Exception  e)
    {
    }

}

Record File and Output

这是因为您再次覆盖了您的文本并再次出现。要获取所有数据,首先从中获取现有文本,然后附加新文本,然后每次都显示新旧文本。

tv.setText(tv.getText().toString() + "new data here!");

对于您的情况,请尝试以下操作:

if(textView.getText()!=null){  
    textview.setText(textView.getText().toString() + "\n" + teamAName.toString() +" "+ teamAScore.toString() + " "+ teamBName.toString()+ " "+teamBScore.toString()+ "\n");
}

改为:

FileInputStream fin = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/courtrecord.txt");
DataInputStream din = new DataInputStream(fin);
String fromfile=din.readLine();
textview.setText(fromfile);

StringBuilder stringBuilder = new StringBuilder();

while(( fromfile  =  din.readLine())!=null)
{
    String  teamAName  = fromfile.substring(0,fromfile.indexOf('@'));
    String teamAScore = fromfile.substring(fromfile.indexOf('@')+1,fromfile.indexOf('#'));
    String teamBName = fromfile.substring(fromfile.indexOf('#')+1,fromfile.indexOf('$'));
    String teamBScore = fromfile.substring(fromfile.indexOf('$')+1,fromfile.indexOf('%'));
    // 0-@,  @-#, #-$, $-%
    final String s = " " + teamAName.toString() + " " + teamAScore.toString() + " " + teamBName.toString() + " " + teamBScore.toString() + "\n";
    stringBuilder.append(s);
}
textview.setText(stringBuilder.toString());