开发者

AlertDialog is not created even though I invoke onCreateDialog on Android

开发者 https://www.devze.com 2023-04-01 08:10 出处:网络
I am facing the following problem. I have an image Gallery and I want to open an AlertDialog to display some information regarding to the clicked image. The first time I click on the image, it runs j

I am facing the following problem.

I have an image Gallery and I want to open an AlertDialog to display some information regarding to the clicked image. The first time I click on the image, it runs just fine. But the following times, even though I click on a different image, the same dialog comes up again.

UserCase

When I follow this User Case

  1. Choose an image in position 2
  2. Close the Dialog
  3. Choose an Image in position 6
  4. Close the Dialog

, I get these prints:

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

It means that the code is not running through the onCreateDialog

CODE

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

}


Use onPrepareDialog() method to fill dialog with data. If you want dialog to be destroyed you should call removeDialog() method.


I believe that showDialog only calls onCreateDialog once, if the ID is the same. If you want to update an dialog which has already been created you can override onPrepareDialog to update whatever you like before it is shown to the user.


And is ok because if you see the doc, the onCreateDialog is call in first time.

"When a dialog is requested for the first time, Android calls onCreateDialog(int) from your Activity, which is where you should instantiate the Dialog. This callback method is passed the same ID that you passed to showDialog(int). After you create the Dialog, return the object at the end of the method."

You need to know that this will be created once.

Call onPrepareDialog.

"Before the dialog is displayed, Android also calls the optional callback method onPrepareDialog(int, Dialog). Define this method if you want to change any properties of the dialog each time it is opened. This method is called every time a dialog is opened, whereas onCreateDialog(int) is only called the very first time a dialog is opened. If you don't define onPrepareDialog(), then the dialog will remain the same as it was the previous time it was opened. This method is also passed the dialog's ID, along with the Dialog object you created in onCreateDialog()."


You can try this simple android dialog popup library. It is very simple to use on your activity.

just one line of code is enough to create dialog if you dont need complex functionality around.

Pop.on(activity).with().title(R.string.title).body(R.string.body).show();

Just include following lib in your gradle dependencies

dependencies {
    compile 'com.vistrav:pop:2.0'
}
0

精彩评论

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