Reputation: 511
I'm making a simple game where you place tiles to grow hotel chains and increase your wealth strategically. At certain points, you're given the option to choose what kind of hotel you'd like to create and the following dialogBox should be shown.
Here's the Code:
public void convertToHotelDialog(final Tile tile){
if(AVAILABLE_HOTELS.size() != 0){
String[] tempHotel = new String[AVAILABLE_HOTELS.size()];
AlertDialog.Builder hotelDialog = new AlertDialog.Builder(this);
hotelDialog.setTitle(this.getResources().getString(R.string.hotel_dialog_title));
hotelDialog.setMessage(this.getResources().getString(R.string.hotel_dialog_message));
for (int i = 0; i < AVAILABLE_HOTELS.size(); i++) {
tempHotel[i] = new String();
tempHotel[i] = AVAILABLE_HOTELS.get(i);
}
hotelDialog.setItems(tempHotel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
String hotelName = AVAILABLE_HOTELS.get(which);
convertToHotel(tile, getHotelCodeByName(hotelName));
}
});
hotelDialog.show();
} else {
AlertDialog.Builder hotelDialog = new AlertDialog.Builder(this);
hotelDialog.setTitle(this.getResources().getString(R.string.hotel_dialog_title));
hotelDialog.setMessage(this.getResources().getString(R.string.hotel_dialog_fail_message));
hotelDialog.show();
}
}
I've run the debugger and stepped my way through it. It IS stepping through the IF portion of the code but the dialog box shows up completely empty. The title is correct, but the message and the spinner just don't exist.
Perhaps I don't understand completely how setItems works?
Any help would be greatly appreciated. Thanks in advance, JRad
Upvotes: 0
Views: 118
Reputation: 1468
You can use the following method to make a list inside the dialog, i use a method like that to make a list of telephones on my app:
AlertDialog.Builder builderSingle = new AlertDialog.Builder(YourActivity.this);
builderSingle.setIcon(R.drawable.yourIcon);
builderSingle.setTitle(this.getResources().getString(R.string.hotel_dialog_title));
final ArrayAdapter<String> arrayAdapterHotels = new ArrayAdapter<String>(YourActivity.this,android.R.layout.simple_expandable_list_item_1, AVAILABLE_HOTELS);
builderSingle.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builderSingle.setAdapter(arrayAdapterHotels,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
String hotelName = AVAILABLE_HOTELS.get(which);
convertToHotel(tile, getHotelCodeByName(hotelName));
}
});
builderSingle.show();
Hope it helps!
Upvotes: 1