1

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Aryan Sharma
  • 119
  • 1
  • 1
  • 11

2 Answers2

1

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());
  • This is great, After adding you mentioned code, studio asked to put some code in try catch, and running excellent. Thanks – Aryan Sharma May 15 '17 at 07:56
0

It is because you are overwriting your text again and agnain. To get all data, first get the existing text from it, then append the new text with it and then display old and new both text every time.

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

In your case try following:

if(textView.getText()!=null){  
    textview.setText(textView.getText().toString() + "\n" + teamAName.toString() +" "+ teamAScore.toString() + " "+ teamBName.toString()+ " "+teamBScore.toString()+ "\n");
}
Kaushal28
  • 5,377
  • 5
  • 41
  • 72