Tomislav Turcic
Tomislav Turcic

Reputation: 889

Stringbuilder not working properly

I have a weird problem. Here's the code:

public class TaxiDetails extends Activity{

ArrayList<Bus> listBus = new ArrayList<Bus>();
ArrayList<Taxi> listTaxi = new ArrayList<Taxi>();
TextView detalji, title;
ImageView backBtn;
String strType;
StringBuilder builder = new StringBuilder();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    setContentView(R.layout.numbers_details);
    detalji = (TextView) findViewById(R.id.txtNumbDetails);
    title = (TextView) findViewById(R.id.txtTitleNumbers);
    detalji.setTypeface(Typeface.DEFAULT_BOLD);
    Typeface TF = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Black.ttf");
    backBtn = (ImageView) findViewById(R.id.backBtn);
    title.setTypeface(TF);

    Intent in = getIntent();
    strType = in.getStringExtra("type");

    if  (strType.equals("TAXI")){
        listTaxi = (ArrayList<Taxi>) in.getSerializableExtra("TAXI");
        title.setText("Taxi");
    } else if (strType.equals("BUS")){
        listBus = (ArrayList<Bus>) in.getSerializableExtra("BUS");
        title.setText("BUS");
    }   

    if (listBus != null) {
        for (int i = 0; i < listBus.size(); i++) {
            builder = new StringBuilder();
            String naslov = listBus.get(i).getTitle();
            String opis = listBus.get(i).getDescription();
            builder.append(naslov);
            builder.append("\n");
            builder.append(Html.fromHtml(opis));
            builder.append("\n");
        }
    }

    if (listTaxi != null) {
        for (int i = 0; i < listTaxi.size(); i++) {
            builder = new StringBuilder();
            String naslov = listTaxi.get(i).getTitle();
            String opis = listTaxi.get(i).getDescription();
            builder.append(naslov);
            builder.append("\n");
            builder.append(Html.fromHtml(opis));
            builder.append("\n");
        }
    }

    detalji.setText(builder.toString().trim());
}
}

I pass an array from previous activity. Either an array of Taxi or Bus objects, depending on which the user chooses. However, for Taxis, this works perfectly. For Bus, it only shows the last object of the array even though debugger shows that listBus contains 2 objects. I use the same listBus iN Google Map activity and there it works to display the location of all objects, so why doesn't it work here?

Upvotes: 3

Views: 3138

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213233

You are creating a new StringBuilder on each iteration of your loop. It should not have even worked for Taxis too, as you are doing the same thing in there.

Just move the below statement, outside the for loop:

builder = new StringBuilder();

Or, you better delete the assignment, since you have already initialized it in the instance variable declaration. So you don't need it here.

Upvotes: 6

Related Questions