Skip to content Skip to sidebar Skip to footer

Dialog In Custom Adapter In Android

I have one custom adapter that have one checkbox and one textview.I want when user clicked on one row one alert dialog pop up and user enter data.I use following code in activity a

Solution 1:

In Class CustomAdapter you need to declare a class level variable mContext

Context mContext;

Create a constructor:

publicAdapterAudio(Context mContext) {
    super();
    this.mContext = mContext;
}

When you call CustomAdapter from Activity, "Activity_Main.this" is Context you need

CustomAdapteradapter=newCustomAdapter(Activity_Main.this);

Don't pass getApplicationContext(), we need to pass MainActivity.this to the constructor as AlertDialog class need this to show alert.

If you pass getApplicationContext() or getContext() then you will get the following error:

W/System.err: android.view.WindowManager$BadTokenException: Unable toaddwindow-- token null is not for an application

Now, pass the mContext to show alert:

AlertDialog.Builderbuilder=newAlertDialog.Builder(mContext);
    builder.setTitle("floors");
    builder.setView(layout);
    builder.setCancelable(false);
    builder.create();

    flooralert = builder.create();
    flooralert.show();

Solution 2:

Try this:

LayoutInflaterinflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

You are already passing your Context to MyCustomAdapter.

Solution 3:

Simply pass necessary Context to your adapter from activity and use that context in your dailog.

Here@

AlertDialog.Builder builder = new AlertDialog.Builder(here will be your context which isfrom activity);

As you mention in your comment that you have to custom dialog and you have to do findviewById.

you can do like this@ (It just an example)

Updated

AlertDialog.Builderbuilder=newAlertDialog.Builder(your_activity_context);
Viewview= your_activity_context.getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(view);

 Buttonbutton= (Button) view.findViewById(R.id.dialog_ok);

Post a Comment for "Dialog In Custom Adapter In Android"