Get Context Inside Onclick(Dialoginterface V, Int Buttonid)

Get context inside onClick(DialogInterface v, int buttonId)?

You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous class. If you're in an activity you can use MyActivity.this as the context.

Edit - since your Activity is implementing DialogInterface.OnClickListener, you should be able to just use this as the context.

How to get context from on click method in Dialogbox

Because you're in a Anonymous object defintion, you can't actually reference methods that would be otherwise applicable to the Activity. In your case if you want to get Context from within your onClick, then you can do it either one of two ways:

1) Store the Context into a global variable in the onCreate of your Activity so that it can be used by your OnClick

2) Do a explicit reference to your Activity like this in your OnClick:

<ActivityName>.this

Trying to open an Activity from an AlertDialog in a Fragment

AlertDialog automatically dismisses the dialog when the buttons are clicked - i.e., when your setPositiveButton OnClickListener fires. When the dialog is dismissed, the DialogFragment is automatically removed.

Because you're doing asynchronous work, your result returns after the Dialog is dismissed and the DialogFragment is removed. At that point, it is expected that dialog.getContext() would be null and getContext() on the DialogFragment would also be null.

There's no way to change the behavior of AlertDialog to only dismiss after your asynchronous callback completes, but you can hold onto the Context from before you kick off your asynchronous work and use it when your async call completes:

@Override
public void onClick(DialogInterface dialogInterface, int i) {
final Context context = getContext();
autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
FirebaseUser usuario = autenticacao.getCurrentUser();

suario.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(context, "E-mail de redefinição de senha enviado!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, IntroActivity.class);
startActivity(intent);

} else {
Toast.makeText(context, "Ocorreu um erro na exclusão da sua conta", Toast.LENGTH_SHORT).show();
}
}
});

Note that this has one downside: if your Activity is destroyed while your asynchronous work is running, you are continuing to hold onto a reference to the old Activity rather than letting it immediately be garbage collected. Assuming your task does not take very long, this might not be an issue for you.

How to call startActivity from an internal anonymous class of an ArrayAdapter class?

startActivity() is a method of Context so you need to call it on the Context object:

getContext().startActivity(intent);

Note that if the Context was not an Activity, then you would need also to set FLAG_ACTIVITY_NEW_TASK on the Intent. But startActivity() is overridden in Activity to not need that, and you have an activity context here.

Android AlertDialog and PreferenceActivity

Use the normal context (in this case your activity) provided by the view being clicked instead of the application context.

new AlertDialog.Builder(v.getContext())

Alert Dialogue box on image view click listener in android

Change getApplicationContext() to v.getContext()

Associate Dialog's Item on click

The parameter "which" in the callback tells you which button you clicked...

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
final CharSequence[] items ={"Lux", "Vigor"};
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_LONG).show();
}
});
builder.create().show();

Using setOnLongClickListener to setText on multiples of buttons

What's happening is the btn variable is getting reassigned each iteration of your loop. Therefore once the long click listener fires, you are calling showAddItemDialog(this, btn) where btn is holding a reference to whatever it was set to in the last loop iteration (when i = 6).

So the behaviour you're experiencing makes sense. Hopefully this is enough to point you in the right direction.


As a side note, finding views based on dynamic ids that come from r.getIdentifier() might be a bit of a bad design choice and could open up bugs in the future. I would recommend simplifying it to just use R.id.button1, R.id.button2 etc. if possible.



Related Topics



Leave a reply



Submit