【问题标题】:Add onlongclick listener to an alertdialog将 onlongclick 侦听器添加到警报对话框
【发布时间】:2023-04-07 23:24:01
【问题描述】:

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

【问题讨论】:

    标签: android onlongclicklistener


    【解决方案1】:

    添加 OnLongClickListener 的一种方法是覆盖对话框的 OnShowListener 并在 onShow(DialogInterface dialog) 方法中设置 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();
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-03
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多