android - 想要在带有 onClick 监听器和 onLongClickListener 的警报对话框中有一个 ListView

标签 android listview android-alertdialog

要创建带有 ListView 的警报对话框,我使用了以下代码:

                ArrayList<String> namesAL = dbHandler.getArrayListOFnames();
                final ListAdapter m_Adapter = new ArrayAdapter<String>(fragment_console.this,android.R.layout.simple_expandable_list_item_1, namesAL);


                builderSingle.setAdapter(
                        m_Adapter,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {                                     
                                destloc = getLocLatLng(which);
                                destlat = destloc.latitude;
                                destlng = destloc.longitude;
                                gotoLocation(destlat, destlng, 14);
                                if (marker != null) {
                                    marker.remove();
                                }
                                if (circle != null){
                                    circle.remove();
                                    circle = null;
                                }

                                MarkerOptions options = new MarkerOptions()
                                        .title("Your destination")
                                        .position(destloc)
                                        .position(destloc)
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.dest_marker));

                                marker = map.addMarker(options);
                                onDestinationChanged();
                                dialog.cancel();                                   }
                        });
                builderSingle.show();

但这限制了我只能使用OnClickListener,没有长按监听器的选项。我也需要一个长按监听器,以便用户可以从我提供的列表中删除一个条目(实际上仅由用户创建)。如何做到这一点?

最佳答案

            // TODO Auto-generated method stub
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(
                    ListAlertDailog.this);
            alertBuilder.setIcon(R.drawable.ic_launcher);
            alertBuilder.setTitle("Select Mobile OS:-");
            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    ListAlertDailog.this,
                    android.R.layout.select_dialog_item);
            arrayAdapter.add("Android");
            arrayAdapter.add("IOS");
            arrayAdapter.add("Windows");
            arrayAdapter.add("Bada");
            arrayAdapter.add("BlackBerry OS");
            arrayAdapter.add("Symbian OS");

            alertBuilder.setNegativeButton("Cancle",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.dismiss();
                        }
                    });

            alertBuilder.setAdapter(arrayAdapter,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            String strOS = arrayAdapter.getItem(which);
                            Toast.makeText(getApplicationContext(),
                                    "On click selected " + strOS, Toast.LENGTH_SHORT)
                                    .show();
                            dialog.dismiss();
                        }
                    });

            final AlertDialog alertDialog = alertBuilder.create();
            alertDialog.setOnShowListener(new OnShowListener() {

                @Override
                public void onShow(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    ListView listView = alertDialog.getListView(); 
                    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

                        @Override
                        public boolean onItemLongClick(
                                AdapterView<?> parent, View view,
                                int position, long id) {
                            // TODO Auto-generated method stub
                            String strOS = arrayAdapter.getItem(position);
                            Toast.makeText(getApplicationContext(),
                                    "Long Press - Deleted Entry " + strOS,
                                    Toast.LENGTH_SHORT).show();
                            alertDialog.dismiss();
                            return true;
                        }
                    });
                }
            });

            alertDialog.show();

关于android - 想要在带有 onClick 监听器和 onLongClickListener 的警报对话框中有一个 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38202987/

相关文章:

java - 如何将 "ellipsize"AlertDialog的标题放在中间?

android - Google Play 游戏和个人资料图片

android - 向自定义 ArrayAdapter 添加更多项目

android - 使用 ListAdapter 使用 simple_list_item_2 在 ListView 中显示联系人姓名和号码,如何在 onItemClick() 中仅提取号码?

android - 检查 ListView 项目是否包含指定数据然后开始下一个 Activity

android - 具有 AlertDialog 样式的对话框

java - ANDROID:将输入的数字转换为单词

java - Android相对布局问题

Android 使用 libavcodec 为 ARGB 编码 h264

android - 什么是 android.R.id.message?