我有一个奇怪的问题。这是代码:
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());
}
}
我从之前的活动传递数组。可以是Taxi或Bus对象的数组,具体取决于用户选择的对象。然而,对于出租车来说,这非常有效。对于Bus,它只显示数组的最后一个对象,即使调试器显示listBus包含2个对象。我使用相同的listBus iN谷歌地图活动,它在那里显示所有对象的位置,那么为什么它不在这里工作?
答案 0 :(得分:5)
您正在循环的每次迭代中创建一个新的StringBuilder
。它甚至不应该用于Taxis
,因为你在那里做同样的事情。
在for循环之外移动以下语句:
builder = new StringBuilder();
或者,您最好删除赋值,因为您已在实例变量声明中初始化它。所以你在这里不需要它。