加入onlongclick监听到alertdialog [英] Add onlongclick listener to an alertdialog

查看:99
本文介绍了加入onlongclick监听到alertdialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AlertDialog Android中包含来自sqlite的好友列表。当我在列表中点击好友的名字,那哥们叫。我想要做的就是添加一个longclicklistener到列表中还有这样我就可以提示删除列表中的好友。我有有onlclick和onlongclick工作在相同的元素麻烦。有人可以给我一个指针在这里。我一直在与Android的几个月。感谢您的帮助!

 私人无效displayBuddyList(字符串区){
        最后弦乐区域2 =区域;
        上下文的背景下= getApplicationContext();
        DH =新DataBaseHelper(上下文);

        名单<字符串> bnames = dh.selectBuddies();
        Log.d(TAG,巴迪名称:+ bnames);



    最后的CharSequence []芽= bnames.toArray(新的CharSequence [bnames.size());
//最后的CharSequence []项目= {标记,维克兰特号,欧莱简,丹};

    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    builder.setTitle(选择好友);
    builder.setItems(嫩芽,新DialogInterface.OnClickListener(){



        公共无效的onClick(DialogInterface dialogInterface,INT项){

        // showShortToast(点击+好友[项目]);
            字符串PT code =芽[项目]的ToString();;




        如果(区域2 ==A){

                callbuddy(PT code,区域2);

            }否则,如果(区域2 ==E){

                        callbuddy(PT code,区域2);


           }否则,如果(区域2 ==P){

                        callbuddy(PT code,区域2);



            } 其他 {
                 showShortToast(我们有一个bug);
            }

           返回;
        }
    });
    builder.create()显示()。
}
 

解决方案

添加一个OnLongClickListener一种方法是通过重写对话框的OnShowListener从OnShow中(DialogInterface对话)方法中设置的OnItemLongClickListener。试试这个:

 私人无效displayBuddyList(字符串区){
    最后弦乐区域2 =区域;
    上下文的背景下= getApplicationContext();
    DH =新DataBaseHelper(上下文);
    名单<字符串> bnames = dh.selectBuddies();
    Log.d(TAG,巴迪名称:+ bnames);

最后的CharSequence []芽= bnames.toArray(新的CharSequence [bnames.size());
//最后的CharSequence []项目= {标记,维克兰特号,欧莱简,丹};

AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setTitle(选择好友);
builder.setItems(嫩芽,新DialogInterface.OnClickListener()
{
    公共无效的onClick(DialogInterface dialogInterface,INT项){
    // showShortToast(点击+好友[项目]);
        字符串PT code =芽[项目]的ToString();;
    如果(区域2 ==A){
            callbuddy(PT code,区域2);
        }否则,如果(区域2 ==E){
                    callbuddy(PT code,区域2);
       }否则,如果(区域2 ==P){
                    callbuddy(PT code,区域2);
        } 其他 {
             showShortToast(我们有一个bug);
        }
       返回;
    }
});

最后AlertDialog广告= builder.create(); //不显示对话框尚未
ad.setOnShowListener(新OnShowListener()
{
    @覆盖
公共无效OnShow中(DialogInterface对话框)
{
        ListView的LV = ad.getListView(); //这是一个ListView在它的芽
        lv.setOnItemLongClickListener(新OnItemLongClickListener()
    {
    @覆盖
    公共布尔onItemLongClick(适配器视图<>母公司视图中查看,INT位置,长ID)
    {
        Log.d(!长按,列表编号+位置+长期被点击);
        返回true;
    }
    });
}
});
ad.show();
 

}

I have an AlertDialog in android that contains a list of buddies from sqlite. When I click on the buddy name in the list, that buddy is called. What I want to do is add a longclicklistener to the list as well so I can be prompted to delete the buddies in the list. I am having trouble having onlclick and onlongclick work on the same element. Can someone give me a pointer here. I have been working with android for a few months. Thanks for any help!

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

解决方案

one way to add an OnLongClickListener is by overriding the dialog's OnShowListener and setting an OnItemLongClickListener from within the onShow(DialogInterface dialog) method. Give this a try:

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

}

这篇关于加入onlongclick监听到alertdialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆