1

I am trying to show the confirmation dialog after clicking on the button in listview.

I have setOnClickListener in my CustomAdapter in getView method.

but on click listener I am getting following error:

02-25 21:36:32.065 20631-20631/com.themsg.chat W/Toast: From com.themsg.chat, go ahead.
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:569)
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:266)
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.app.Dialog.show(Dialog.java:286)

Here is my code:

holder.tvm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    chatroomMembers = getItem(position);

                    Toast.makeText(getContext(), "here", Toast.LENGTH_LONG).show();
                    new AlertDialog.Builder(v.getContext())
                        .setTitle("Title")
                        .setMessage("Do you really want to whatever?")
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                deleteUserFromChatrrom(chatroomMembers.getId(), SessionData.getInstance().getCurrentChatroom(), position);
                            }})
                        .setNegativeButton(android.R.string.no, null).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
Albert
  • 63
  • 1
  • 7
  • why you are not getting error `The method getContext() is undefined for the type new View.OnClickListener(){}` in your toast line... – Iamat8 Feb 25 '16 at 16:41
  • i mean you there is not method `getContext()` in `View.OnClickListener()` – Iamat8 Feb 25 '16 at 16:42

2 Answers2

2

In Class CustomAdapter you declare a variable mContext and a ArrayList data to ListView

    ArrayList<String> datasource;
    Context mContext;

Create a constructor:

    public AdapterAudio(Context mContext, ArrayList<String> data) {
    super();
    this.datasoure = data;
    this.mContext = mContext;
}

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

   CustomAdapter adapter = new CustomAdapter(Activity_Main.this, listAudio_hienthi10);

Now you have a Context, use variable mContext is declared to replace

    "getContext()", "v.getContext()"

Now you can Toast or show any dialog when click Button in CustomAdapter you want. Enjoy your code!

nhatpv
  • 93
  • 1
  • 10
0

Try changing getContext() with getApplicationContext()

malrok44
  • 592
  • 6
  • 17