Reputation: 954
I'm trying to cycle through a JSONObject to see how many passengers there are, and then I want to display them all nicely in a dialog window. It looks like there are a few ways to do it, but overall I'm just confused on how to go about it. This is the closest I've gotten, it works but as you can see I'm only adding one item. So there is only one item displaying in my alertdialog, and there should be a couple more. My alertdialog (called ticketbuilder) is created somewhere else, I'm just trying to add everything in this for loop. How do I add all of my passengers to the list to display? Thanks a ton in advance!
for (int i = 0; i < tickets.length(); i++) {
final int ticketCount = i;
JSONObject ticket;
try {
ticket = tickets.getJSONObject(ticketCount);
passengername = ticket.getString("passengername");
ticketnumber = ticket.getString("ticketnumber");
CharSequence[] array = {passengername + " \n" + ticketnumber};
ticketBuilder.setItems(array, null); //adding to my dialog
} catch (JSONException e) {
System.out.println(e);
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 0
Views: 304
Reputation: 5935
Try putting the CharSequence outside the loop and init it like this:
CharSequence[] array = new CharSequence[tickets.length()];
Then in the loop add thing to the array:
array[i] = {passengername + " \n" + ticketnumber};
Move ticketBuilder.setItems so it is after, outside the loop.
Upvotes: 1