TextView settext显示单行而不是全部

时间:2017-05-15 07:44:26

标签: android textview fileinputstream datainputstream

上下文: 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

2 个答案:

答案 0 :(得分:1)

改为:

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

答案 1 :(得分:0)

这是因为你再次覆盖了你的文字并且不知道了。要获取所有数据,首先从中获取现有文本,然后使用它附加新文本,然后每次都显示旧的和新的两个文本。

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