私のアプリでは、ユーザーは新しい都市を追加できます。これは次のようになります。AlertDialogにリストされている項目を変更するにはどうすればよいですか?
- メニューから[都市を追加]ボタンを押します。
- 表示されるリスト(AlertDialog)から地域を選択します。
- 選択した都道府県に基づいて作成されたリストを都市から選択します。
州のダイアログは常に同じであるため、都市ダイアログに別のダイアログを作成しました。 問題は、onCreateDialog()は都市が初めて追加されたときにのみ呼び出されるため、選択した地域に基づいてこのリストを調整する方法がわかりません。 addItems()は、AlertDialog.builderのメソッドです。私が知る限り、onPrepareDialogではあまり役に立ちません。 ?私はダイアログで、それが呼び出されるたびにアイテムのリストを変更する(とonClickListenerを適宜更新するにはどうすればよい
編集:私は、私がこれまで持っているコードを追加した2つの主要な問題があります。私の現在の実装で:
- 市ダイアログ(SelectLocationDialogは)州ダイアログから完全に違って見える私は彼らが(私はandroid.R.layout.simple_list_item_1を使用しています同じに見えるようにする方法を見つけ出すことはできません。今の市のダイアログ)
onClickListはありません市のダイアログのためのener、それは本当に何かをほとんど行いません。
@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(); } }
私はこれまでのことと、実際にはうまくいかない理由の2つを追加しました。 – Dean