1

中顯示來自listview按鈕的提醒確認對話框我試圖在點擊列表視圖中的按鈕之後顯示確認對話框如何在android

我在getView方法setOnClickListenerCustomAdapter

但點擊收聽我收到以下錯誤:

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) 

這裏是我的代碼:

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

爲什麼你沒有得到錯誤'方法getContext()是未定義的類型新View.OnClickListener(){}'在你的吐司行... – Mohit

+0

我的意思是你沒有方法'getContext()'在'View.OnClickListener()' – Mohit

回答

2

改變的getContext()在類CustomAdapter你向ListView聲明變量mContext和ArrayList數據

ArrayList<String> datasource; 
    Context mContext; 

創建一個構造函數:

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

當您從活動叫CustomAdapter, 「Activity_Main.this」 是上下文,你需要

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

現在你有一個背景下,使用可變mContext被聲明爲替換

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

現在,您可以在單擊您想要的CustomAdapter中的Button時點擊Toast或顯示任何對話框。 享受你的代碼!

0

嘗試用getApplicationContext()