Search code examples
androidtextviewfileinputstreamdatainputstream

TextView settext showing single line instead of all


Context: textview should display all the saved data in file, which is in the form of lines

Problem: Displaying only current data not the previous all records.

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


Solution

  • change to this:

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