Ahmad
Ahmad

Reputation: 72533

Displaying more than one String Array in Android

I am currently building an Book-App (sort of). So there are a lot of Strings, and the User can choose from which line he wants to start reading. So e.g there are 200 Strings in a String Array for Chapter one. And the User wants to start reading at Line 20(because he read the Lines before already). How can I display all the Strings from 20-200? I have got:

    Resources res = getResources();
    String[] Book= res.getStringArray(R.array.ChapterOne);
    TextView ChapterOne= (TextView) findViewById(R.id.Text);
    SuraAlFateha.setText(Book[20]);

But This just diplays the Line 20. But I want it to Display All the Lines following from 20 (20-200). How can I do this? Thank you.

Upvotes: 0

Views: 88

Answers (2)

Karl Rosaen
Karl Rosaen

Reputation: 4644

Book[20] will simply give you the 20th element of the array, if you wish to get text for 20 through the end, you'll need to join the range of elements into a string.

You can use text and array utils to make this easy.

String joinedLines = TextUtils.join("\n", java.utils.Arrays.copyOfRange(Book, 20, Book.length));
SuraAlFateha.setText(joinedLines);

Upvotes: 1

Andreas Hagen
Andreas Hagen

Reputation: 2325

When you set the text you need to set all the text at once, like:

String resultString = '';

for ( int i = 20; i < Book.length; i++ )
    resultString = resultString + "\n" + Book[i];

SuraAlFateha.setText( resultString.substring( 1 ) );

or something similar.

You should however calculate how much space you need and reserve it before starting the string appending or else your runtime and memory usage might get sky high.

Upvotes: 1

Related Questions