1

I have the following loop which creates a String which fits exactly in a screen, it creates a page so to speak.

    for (int i = 0; i < totalLine; i++) {

        temp = enterLine(mTextView, textToBeShown, i);
            full = full + temp;

    }

So after the iteration is done full holds one page.

What i want to do is create an outer iteration which allows me to create more than one page, the amount of pages to be created isn't defined. So the iteration needs to stop if there aren't more pages to be created.

I tried the following code but for some reason when calling a page Pages.get(1) it gives out the whole String not just the full / Page. For Example if i three strings have been added to the ArrayList Pages there will be three Strings in the ArrayList but all with the same value.

With some testing with the Log, i know that the first iteration is working well, and that full gives the expected values meaning in the first do iteration gives out the expected values to the full so does the second iteration etc..

    do{
    for (int i = 0; i < totalLine; i++) {

        temp = enterLine(mTextView, textToBeShown, i);
        if(temp.trim().length() == 0){
            break;
        }else{
            full = full + temp;
        }
    }
    Pages.add(full);

So the question is what am i doing wrong with the ArrayList and why isn't it working as I'm expecting.

Edit

This is the enterLine code: More Log's were used didn't feel the need the display them all.

public String enterLine(TextView mTextView, String textBeShown, int i){

        String A;
        int number = mTextView.getPaint().breakText(textToBeShown, 0, textToBeShown.length(),true,mTextView.getWidth(), null);

        if(textToBeShown.substring(number-1,number) != " "){
            number = textToBeShown.substring(0,number).lastIndexOf(" ");
            Log.e("TAG1", "Found " + i);
        }

        A = (textToBeShown.substring(0, number) + "\n");
        Log.e(TAG, textToBeShown.substring(0, number));
        textToBeShown = textToBeShown.substring(number, textToBeShown.length());
        return A;
    }
8
  • 1
    post the code for enterLine, not sure what that is doing... Commented Apr 25, 2013 at 14:56
  • I will, but the enterLine works correctly, altough it can be connected to it. Commented Apr 25, 2013 at 14:58
  • You need to re-initialize full to an empty value somewhere, no? You keep filling it. Would it be a good idea to reset it bewteen do and for? Commented Apr 25, 2013 at 15:04
  • 1
    Where is the while for the do? Also, have full be a StringBuilder, not a String. Commented Apr 25, 2013 at 15:07
  • Not completely related to your question but it's better practice to use a StringBuilder when you build up a String over a loop like that. Commented Apr 25, 2013 at 15:09

2 Answers 2

1
do{
    full=""
    for (int i = 0; i < totalLine; i++) {
        temp = enterLine(mTextView, textToBeShown, i);
        if(temp.trim().length() == 0){
            break;
        }else{
            full = full + temp;
        }
    }
    Pages.add(full);
}while(...)

or better

do{
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < totalLine; i++) {
        temp = enterLine(mTextView, textToBeShown, i));
        if(temp.trim().length() == 0){
            break;
        }else{
            builder.append(temp);
        }
    }
    Pages.add(builder.toString());
}while(...)
Sign up to request clarification or add additional context in comments.

Comments

1

From the looks of it, it's not your arraylist but your loop. Add adds an element to the arraylist, get(index) gets the index'th element from the list. No problem there.

Your problem is that it adds full to the page after the loop, by which point full already contains everything. Put the pages.add inside the loop and it'll be fixed. Make sure you reset full each iteration. Put full = "" at the start of the loop. Should work then.

4 Comments

I'm sorry it may be my fault that i didn't explain it well, but when i goes from 0 till totalLine it creates one full page. than in the do..while it creates the pages. In each of the for..loop's iteration a line that fits the width of the screen is being created.
Ah. Well then you'll still want to reinitialize full at the start of each loop, as it is it's just going to keep collecting all the text into one string and adding ever longer strings to pages. Try that, let me know how it goes.
Yes, someone had already suggested it and it worked thanks for the help!
I did in the original post as well, "Put full = "" at the start of the loop". But I'm glad you got it working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.