我面临以下问题。
我有一个图像Gallery
并且我想打开一个AlertDialog
来显示有关单击图像的一些信息。我第一次点击图片时,它运行得很好。但是在接下来的时间里,即使我点击了一个不同的图片,同样的对话框再次出现。
用户名
当我关注这个用户案例时
在位置2中选择图像
关闭对话框
选择位置6中的图像
关闭对话框
,我得到这些指纹:
click na galeria... position: 2
startUserInformationDialog()... `this.position`: 2
onCreateDialog on switch.. `this.position`: 2
DISMISSING DIALOG `this.position`: 2
click na galeria... position: 6
startUserInformationDialog()... `this.position`: 6
DISMISSING DIALOG `this.position`: 6
这意味着代码没有通过oncreatedialog运行
代码
public AlertDialog createUserInformationAlertDialog() {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_user_info,
(ViewGroup) findViewById(R.id.dialog_user_layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
final MyObject ea = myList.get(positionUserToHaveInformationDisplayedOnTheDialog);
// Setting values according to element to be shown
TextView textView = (TextView) dialoglayout
.findViewById(R.id.user_name_value);
textView.setText(ea.getName());
...
builder.setNegativeButton(Locale_PT_BR.BACK,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Log.d("PRINT 1",
"DISMISSING DIALOG `this.position`: "
+ positionUserToHaveInformationDisplayedOnTheDialog);
dialog.dismiss();
}
});
return builder.create();
}
private void startUserInformationDialog() {
Log.d("PRINT 2", "startUserInformationDialog()... `this.position`: "
+ positionUserToHaveInformationDisplayedOnTheDialog);
showDialog(DIALOG_USER_INFORMATION);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_USER_INFORMATION:
Log.d("PRINT 3", "onCreateDialog on switch.. `this.position`: "
+ positionUserToHaveInformationDisplayedOnTheDialog);
return createUserInformationAlertDialog();
default:
return null;
}
}
private void startGallery() {
myPhotoBar = (Gallery) findViewById(R.id.gallery);
myPhotoBar.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
Log.d("PRINT 4", "click na galeria... position: " + position);
positionUserToHaveInformationDisplayedOnTheDialog = position;
startUserInformationDialog();
}
});
}
最佳答案
使用onPrepareDialog()
方法用数据填充对话框。如果要销毁对话框,应该调用removeDialog()
方法。