我在Android for Java编程方面很陌生,我可能错过了一个让我无法做到我想做的事情。
基本上,我想启动两个对话框,一个是另一个。换句话说:当第一个关闭时,我希望第二个打开。
你会理解它的使用:
我有两个字段(主要电话号码和主要邮件地址)的活动。我正在使用一个很好的ContactPicker活动,并将我带回正确的ID。当联系人有多个电话号码时,我打开一个AlertDialog,我将数字列表传递给我,我让用户选择了主号码。更新了正确的字段后,我想启动第二个字段,让用户选择主邮件地址。 关键是:我找不到任何触发新AlertDialog的事件:
目前,我已经尝试过:活动OnResume,Dialog onDismiss,但没有成功(第一个似乎是正常的,第二个可能是错误使用AlertDialog的调用。
我在论坛上读过一个好的做法是使用新的活动。在我看来,单个列表的显示有点贵,但如果我找不到任何其他答案,我会继续这样做。
我能看到的解决方案:
所以,也许我会错过Android哲学中的一些东西。你能帮我吗?
以下是代码的摘录(感谢通过互联网挑选的一些代码):
// CATCH ContactPicker Result in order to fetch the contact ID
// and fill the fields
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
CharSequence[] itemsList = null;
MyContactsOperations contact = null; //to get contact's details
String id=null;
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
try {
Uri result = data.getData();
// get the contact id from the Uri
id = result.getLastPathSegment();
} catch (Exception e) {
Log.e(DEBUG_TAG, "Unable to get contact infos", e);
} finally {
setEditTexts(EDIT_PHONE_NUMBER, id); //use the contact's details to fill the choosen field
}
} // END GET PHONE NUMBER
}
}
private void setEditTexts(int editTextIdx, String idContactPicker) {
CharSequence[] itemsList = null;
MyContactsOperations contact = null;
switch (editTextIdx) {
/*
* Get Phone Number from selected contact
*/
case EDIT_PHONE_NUMBER :
try {
contact= new MyContactsOperations();
itemsList = contact.getPhoneNumbers(this.getApplicationContext(), idContactPicker);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Unable to get contact infos", e);
} finally {
if (itemsList[0]!=null) {
if (itemsList.length==1) {
mNumber = itemsList[0].toString();
setPhoneNumber ();
}
else {
//Launch an AlertDialog Box to choose the right phone number
selectFromList(itemsList, EDIT_PHONE_NUMBER, idContactPicker);
}
}
}
break;
case EDIT_EMAIL :
try {
contact= new MyContactsOperations();
itemsList = contact.getEMails(this.getApplicationContext(), idContactPicker);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Unable to get contact infos", e);
} finally {
if (itemsList[0]!=null) {
if (itemsList.length==1) {
mEmail = itemsList[0].toString();
setEMail();
}
else {
//Launch an AlertDialog Box to choose the right phone number
selectFromList(itemsList, EDIT_EMAIL, idContactPicker);
}
}
}
break;
}
}
// Method to display the required AlertDialog Box
public void selectFromList(CharSequence[] list, int typeOfList, String id) {
final CharSequence[] itemsList = list;
final int type = typeOfList;
final String idContactPicker = id;
// build and show a simple dialog that allows the user to select a number
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (type) {
case EDIT_PHONE_NUMBER :
builder.setTitle(R.string.tvMultipleMobile);
break;
case EDIT_EMAIL :
builder.setTitle(R.string.tvMultipleeMail);
break;
}
builder.setItems(list, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int itemIdx) {
String uniqueItem = (String) itemsList[itemIdx];
int startOfItemIndex = uniqueItem.indexOf(":");
switch (type) {
case EDIT_PHONE_NUMBER :
mNumber = uniqueItem.substring(startOfItemIndex + 2);
setPhoneNumber ();
setEditTexts(EDIT_EMAIL, idContactPicker);
case EDIT_EMAIL :
mEmail = uniqueItem.substring(startOfItemIndex + 2);
setEMail ();
}
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity(this);
alert.show();
}
public void setPhoneNumber () {
EditText telEntry = (EditText) findViewById(R.id.editText2);
telEntry.setText(mNumber);
}
public void setEMail () {
EditText mailEntry = (EditText) findViewById(R.id.editText3);
mailEntry.setText(mEmail);
}
// AND I DON'T KNOW WHERE TO CALL : setEditTexts(EDIT_EMAIL, id);
但我不知道如何为多个电子邮件启动下一个AlertDialog。
提前致谢,
eflob