将onlongclick监听器添加到alertdialog

时间:2012-02-04 23:39:33

标签: android onlongclicklistener

我在android中有一个AlertDialog,其中包含来自sqlite的好友列表。当我点击列表中的好友名称时,会调用该好友。我想要做的是在列表中添加一个longclicklistener,以便我可以提示删除列表中的好友。我无法使用onlclick和onlongclick工作在同一个元素上。有人可以给我一个指针。我已经和android一起工作了几个月。谢谢你的帮助!

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

1 个答案:

答案 0 :(得分:9)

添加OnLongClickListener的一种方法是重写对话框的OnShowListener并在onShow(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();

}