2012-02-04 87 views
4

我在android中有一個AlertDialog,其中包含來自sqlite的好友列表。當我點擊列表中的好友名稱時,該好友被調用。我想要做的是將一個longclicklistener添加到列表中,以便我可以提示刪除列表中的好友。 onlclick和onlongclick在同一元素上工作時遇到問題。有人可以在這裏給我一個指針。我一直在使用android幾個月。謝謝你的幫助!以添加OnLongClickListenerAdd onlongclick listener to alertdialog

private void displayBuddyList(String region) { 
     final String region2 = region; 
     Context context = getApplicationContext(); 
     dh = new DataBaseHelper(context); 

     List<String> bnames = dh.selectBuddies(); 
     Log.d(TAG, "Buddy Names: " +bnames); 



    final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]); 
// final CharSequence[] items = {"Mark", "Vikrant", "Olle,"Jane","Dan"}; 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Select a Buddy"); 
    builder.setItems(buds, new DialogInterface.OnClickListener() { 



     public void onClick(DialogInterface dialogInterface, int item) { 

     // showShortToast("Clicked on:"+buddy[item]); 
      String ptcode = buds[item].toString();; 




     if (region2 == "A") { 

       callbuddy(ptcode,region2); 

      } else if (region2 == "E") { 

         callbuddy(ptcode,region2); 


      } else if (region2 == "P") { 

         callbuddy(ptcode,region2); 



      } else { 
       showShortToast("We have a bug"); 
      } 

      return; 
     } 
    }); 
    builder.create().show(); 
} 

回答

9

的一種方式是通過重寫對話框的OnShowListener並從昂秀(DialogInterface對話框)方法內設置一個OnItemLongClickListener。這給一試:

private void displayBuddyList(String region) { 
    final String region2 = region; 
    Context context = getApplicationContext(); 
    dh = new DataBaseHelper(context); 
    List<String> bnames = dh.selectBuddies(); 
    Log.d(TAG, "Buddy Names: " +bnames); 

final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]); 
// final CharSequence[] items = {"Mark", "Vikrant", "Olle,"Jane","Dan"}; 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Select a Buddy"); 
builder.setItems(buds, new DialogInterface.OnClickListener() 
{ 
    public void onClick(DialogInterface dialogInterface, int item) { 
    // showShortToast("Clicked on:"+buddy[item]); 
     String ptcode = buds[item].toString();; 
    if (region2 == "A") { 
      callbuddy(ptcode,region2); 
     } else if (region2 == "E") { 
        callbuddy(ptcode,region2); 
     } else if (region2 == "P") { 
        callbuddy(ptcode,region2); 
     } else { 
      showShortToast("We have a bug"); 
     } 
     return; 
    } 
}); 

final AlertDialog ad = builder.create(); //don't show dialog yet 
ad.setOnShowListener(new OnShowListener() 
{  
    @Override 
public void onShow(DialogInterface dialog) 
{  
     ListView lv = ad.getListView(); //this is a ListView with your "buds" in it 
     lv.setOnItemLongClickListener(new OnItemLongClickListener() 
    { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
    { 
     Log.d("Long Click!","List Item #"+position+"was long clicked"); 
     return true; 
    }   
    });  
} 
}); 
ad.show(); 

}