1
在我的應用程序中,用戶可以添加新的城市。這應該如下:如何更改AlertDialog中列出的項目?
- 按下菜單中的「添加城市」按鈕。
- 從顯示的列表(AlertDialog)中選擇一個省。
- 從基於所選省份創建的列表中選擇一個城市。
省對話框總是一樣的,所以我讓城市對話框成爲一個單獨的對話框。 問題是,因爲onCreateDialog()只是在第一次添加城市時被調用,所以我無法弄清楚如何根據所選省份調整此列表。 addItems()是AlertDialog.builder的一個方法,就我所知,它在onPrepareDialog中用處不大。
如何在每次調用時間改變的項目列表對話框中(和更新onClickListener相應
編輯:?我添加了我到目前爲止的代碼有2個主要問題我目前的執行情況:
- 全市對話框(SelectLocationDialog)看起來與省對話框完全不同的我無法弄清楚如何讓他們看起來完全相同(我使用android.R.layout.simple_list_item_1爲現在的城市對話框)
沒有onClickList因爲這個城市對話很有意思,所以它並沒有做任何事情。
@Override protected Dialog onCreateDialog(int id, Bundle args) { AlertDialog.Builder builder = null; switch (id) { case DIALOG_SELECT_PROVINCE: return SelectProvinceDialog.create(this); case DIALOG_SELECT_LOCATION: return SelectLocationDialog.create(this); default: return null; } } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DIALOG_SELECT_LOCATION: // looks up all cities/sites in the province selected in the // previous dialog siteList = new XmlSiteListReader(this); siteList.findSitesByProvince(Province.valueOf( Province.getAbbreviatedName(selectedProvince))); String[] sites = siteList.siteNames(); ListView siteListView = new ListView(this); ArrayAdapter<CharSequence> siteListAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, sites); siteListView.setAdapter(siteListAdapter); dialog.setContentView(siteListView); } } /** * A dialog that allows the user to select a province/region from which to add * locations to watch. * * @author Dean Morin */ public class SelectProvinceDialog { private static final String[] PROVINCES; static { PROVINCES = new String[Province.values().length]; Province[] provinces = Province.values(); for (int i = 0; i < provinces.length; i++) { PROVINCES[i] = provinces[i].getFullName(); } } /** * Creates the 'Select Province' dialog window. * * @param context The context for this dialog. * @return The constructed dialog. */ public static AlertDialog create(final Context context) { AlertDialog.Builder selectProv = new AlertDialog.Builder(context); selectProv.setTitle("Select Province"); selectProv.setItems(PROVINCES, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { ((WeatherWatch) context).setSelectedProvince(PROVINCES[item]); ((Activity) context) .showDialog(WeatherWatch.DIALOG_SELECT_LOCATION, null); } }); return selectProv.create(); } } public class SelectLocationDialog { /** * Creates the 'Select Location' dialog window. * * @param context The context for this dialog. * @return The constructed dialog. */ public static AlertDialog create(final Context context) { AlertDialog.Builder selectLoc = new AlertDialog.Builder(context); return selectLoc.create(); } }
我已經添加了我迄今爲止以及它沒有真正起作用的兩個原因。 – Dean