2013-06-20 26 views
0

我正在列表中,在此列表中setOnItemLongClickListener編寫代碼部分,用戶長按打開對話框但對話框未打開?請發送任何建議打開對話框?列表項longClickListener打開對話框在android中不工作?

listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
     { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       view.setSelected(true); 
       TextView tv = (TextView) view.findViewById(R.id.txtName); 
       String shipmenttxt = tv.getText().toString(); 
       int b=delete_Message("Delete ", "Do you want delete shipment id", "Delete", "Cancel",shipmenttxt,position); 
       if(b==1){ 
        this.mList.remove(position); 
        adapter.notifyDataSetChanged(); 
       } 
       return true; 

      } 

    }); 

@SuppressWarnings("deprecation") 
    private int delete_Message(String sTitle,String sMessage,String sButton1_Text,String sButton2_Text,final String msg,final int position) 
    { 

     try { 
      alertDialog = new AlertDialog.Builder(getParent()).create(); 
      alertDialog.setTitle(sTitle); 
      alertDialog.setIcon(R.drawable.info); 
      alertDialog.setMessage(sMessage); 
      alertDialog.setButton(sButton1_Text, new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int which) 
       { 
        aa=1; 
        delete(msg); 

        //new LoadDatashipment().execute(); 
        return ; 

       } }); 

      alertDialog.setButton2(sButton2_Text, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        aa=0; 
        //return; 
       }}); 

      alertDialog.show(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return aa; 
    } 

回答

3

如果列表中的任何行項目包含可調焦或可點擊查看,然後點擊監聽器可能無法正常工作

你必須把這個線在自定義列表視圖row_item.xml文件 即android:descendantFocusability="blocksDescendants"

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:descendantFocusability="blocksDescendants" 
//other layout info here ..... 
> 
</LinearLayout> 

我想你需要做的是在顯示你的對話框之前

alertD = alertDialog.create(); 

,並顯示在此例如 http://www.mkyong.com/android/android-alert-dialog-example/

+0

嗨大衛我是編輯代碼 – Narasimha

+0

@Narasimha請檢查編輯回答 –

+0

這個問題解決了感謝您分享信息 – Narasimha

0

alertD.show(); 

檢查試試這個代碼:

listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
     view.setSelected(true); 
     TextView tv = (TextView) view.findViewById(R.id.txtName); 
     String shipmenttxt = tv.getText().toString(); 

     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       switch (which) { 
       case DialogInterface.BUTTON_POSITIVE: 
        //TODO Yes button clicked 
        this.mList.remove(position); 
        adapter.notifyDataSetChanged(); 
        break; 
       case DialogInterface.BUTTON_NEGATIVE: 
        //TODO No button clicked 
        dialog.dismiss(); 
        break; 
       } 
      } 
     }; 
     AlertDialog.Builder builder = new AlertDialog.Builder(Main.this); 
     builder.setMessage("Extract " + rarListView.getItemAtPosition(arg2).toString() + " \n to '" + Environment.getExternalStorageDirectory().toString() + "/AndRar/' folder?") 
      .setPositiveButton("Yes", dialogClickListener) 
      .setNegativeButton("No", dialogClickListener) 
      .show(); 
     return true; 
    } 
});