在我的应用中,用户可以添加新城市。这应该如下:
省对话框将始终相同,因此我将城市对话框设为单独的对话框。 问题是因为onCreateDialog()仅在第一次添加城市时被调用,所以我无法弄清楚如何根据所选的省调整此列表。 addItems()是一个AlertDialog.builder的方法,据我所知,在onPrepareDialog中对我没有多大用处。
每次调用时如何更改对话框中的项目列表(并相应地更新onClickListener?
编辑:我添加了目前为止的代码。我目前的实施有两个主要问题:
城市对话框中没有onClickListener,因此它并没有真正做很多事情。
@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();
}
}
答案 0 :(得分:0)
onPrepareDialog是您可以更改列表值的地方。我之前做过类似的事。如果您需要帮助来编写代码,请发布您的代码。