18

I have a file containing text in separate line.
I want to display line first, and then if I press a button, the second line should be displayed in the TextView and the first line should disappear. Then, if I press it again, the third line should be displayed and so on.

Should I have to use TextSwitcher or anything else? How can I do that?

Michael
  • 53,859
  • 22
  • 133
  • 139
Sunny
  • 14,522
  • 15
  • 84
  • 129

3 Answers3

31

You tagged it as "android-assets" so I'm going to assume your file is in the assets folder. Here:

InputStream in;
BufferedReader reader;
String line;
TextView text;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView) findViewById(R.id.textView1);
    in = this.getAssets().open(<your file>);
    reader = new BufferedReader(new InputStreamReader(in));
    line = reader.readLine();

    text.setText(line);
    Button next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(this);
}

public void onClick(View v){
    line = reader.readLine();
    if (line != null){
        text.setText(line);
    } else {
        //you may want to close the file now since there's nothing more to be done here.
    }
}

Give this a try. I haven't been able to verify that it works completely, but I believe this is the general idea you want to follow. Naturally you'll want to replace any R.id.textView1/button1 with the names that you've specified in your layout file.

Also: There is very little error checking here for the sake of space. You will want to check that your asset exists and I'm pretty sure there should be an try/catch block when you open the file for reading.

Edit: Big error, It's not R.layout, it's R.id I have edited my answer to fix the problem.

Otra
  • 8,108
  • 3
  • 34
  • 49
16

Following code should fulfil your need

try {
// open the file for reading
InputStream instream = new FileInputStream("myfilename.txt");

// if file the available for reading
if (instream != null) {
  // prepare the file for reading
  InputStreamReader inputreader = new InputStreamReader(instream);
  BufferedReader buffreader = new BufferedReader(inputreader);

  String line;

  // read every line of the file into the line-variable, on line at the time
  do {
     line = buffreader.readLine();
    // do something with the line 
  } while (line != null);

}
} catch (Exception ex) {
    // print stack trace.
} finally {
// close the file.
instream.close();
}
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Where do you get the `openFileInput()`-method from? Also, you should always use a "try/finally`-block to close streams (so they get closed when an Exception is thrown). – Lukas Knuth Aug 24 '11 at 12:20
  • 1
    Correct methodology, but you've used C-style conditions which won't compile. Java doesn't allow automatic conversion from null/int/assignment etc. to boolean, so `if (instream)` and `while ( line = buffreader.readLine() )` need to be replaced with something like `if (instream != null)` and `while( buffreader.hasNext() )` – epochengine Aug 24 '11 at 12:26
  • 1
    BufferedReader doesn't have a hasNext() function. Just check if it's null. – Error 454 May 21 '13 at 21:53
0

You can simply use a TextView and a ButtonView. Read the file using a BufferedReader, it will provide you with a nice API to read lines one by one. On click on the button just change the text of the textview by using settext.

You could also consider reading all the file content and putting it inside a list of strings, this can be cleaner if your file is not too big.

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173