开发者

How do I alter the items listed in an AlertDialog?

开发者 https://www.devze.com 2023-04-05 04:41 出处:网络
In my app the user can add new cities. This should go as follows: Presses the \'add city\' button from the menu.

In my app the user can add new cities. This should go as follows:

  1. Presses the 'add city' button from the menu.
  2. Selects a province from the list (AlertDialog) that appears.
  3. Selects a city from the list that is created based on the selected province.

The province dialog will always be the same, so I made the city dialog a separate dialog. The problem is that since onCreateDialog() is only called the first time a city is added, I can't figure out how to adjust this list based on the province selected. addItems() is a method of AlertDialog.builder, which isn't of much use to me in onPrepareDialog as far as I can tell.

How do I alter the list of items in the dialog each time it is called (and update the onClickListener accordingly?

Edit: I've added the code that I have so far. There are 2 major issues with my current implementation:

  1. The city dialog (SelectLocationDialog) looks completely different from the province dialog. I can't figure out how to make them look identical (I'm using android.R.layout.simple_list_item_1 for the city dialog for now).
  2. There is no onClickListener for the city dialog, so it doesn't really do much of anything.

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


onPrepareDialog is the place you'll be able to change the values of the list. I've done something similar before. Post your code if you need help to write it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号