2014-02-25 55 views
0

我正在製作一個簡單的遊戲,您可以放置​​瓷磚來增加連鎖酒店並在戰略上增加您的財富。在某些情況下,您可以選擇選擇想要創建的酒店類型,並顯示以下對話框。AlertDialog麻煩(空?)

下面的代碼:

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(); 
    } 
} 

我已經運行調試,並通過它踩我的方式。它正在遍歷代碼的一部分,但對話框顯示爲完全空白。 標題正確,但消息和微調只是不存在。

也許我完全不明白setItems是如何工作的?

任何幫助將不勝感激。在此先感謝, JRad

回答

1

您可以使用下面的方法來讓對話框中的列表,我用這樣的方法,使我的應用程序的電話清單:

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(); 

希望它可以幫助!

+0

哇。這很順利。我取消了取消按鈕,因爲他們必須選擇一家酒店,但這很好。非常感謝。如果你不介意,我有點想知道爲什麼我的代碼不能像例外那樣工作。它是如何變成空白的?瞭解他們爲什麼會幫助我在未來避免這類問題。 –

+0

其實我不知道,因爲我從來沒有使用setItems方法,我一直使用我在我的答案張貼的方式。 – Giacomoni