2011-03-04 112 views
0

有人能指出一個自定義對話框的示例,它將ArrayAdapter作爲輸入並顯示可選列表。Android自定義列表對話框

我曾嘗試使用AlertDialog Builder作爲這樣來創建一個對話框......

final ArrayAdapter<MyObject> myAdapter = getMyobjects(); 
      final AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("Pick an item").setAdapter(myAdapter, 
        new android.content.DialogInterface.OnClickListener() { 
         public void onClick(final DialogInterface dialog, final int item) { 
          Toast.makeText(Islands.this, myAdapter.getItem(item).toString(), Toast.LENGTH_SHORT).show();  
         } 
        }); 
      final AlertDialog alert = builder.create(); 
      return alert; 

我的問題是,我的對話並沒有更新的話,我叫

@Override 
protected void onPrepareDialog(final int id, final Dialog dialog) { 
    switch (id) { 
     case DIALOG_GET_AVAIL_DESTS: 
     ((AlertDialog) dialog).getListView().setAdapter(getDestinations()); 
     break; 
    } 
} 

然而聽衆的onClick聽最初的一套物品...

回答

0

你要撥打電話到

invalidateViews()

你的列表視圖 - 這將導致其重繪與更新的觀點。

0

由於您使用的是onPrepareDialog(int id, Dialog dialog),我猜你最初在onCreateDialog(int id)中設置了對話框。

這樣做會導致系統保存您最初創建的對話框。爲了達到所需的功能,當對話被解除時,通過調用android.app.Activity.removeDialog(int id)告訴系統放棄它。

任何後續調用將使您的對話框通過onCreateDialog(int id)方法重新生成,導致更新項目集。