【问题标题】:Want to have a list view in an alert dialog box with both onClick listener and onLongClickListener希望在带有 onClick 侦听器和 onLongClickListener 的警报对话框中有一个列表视图
【发布时间】:2016-07-05 12:03:11
【问题描述】:

为了创建一个带有列表视图的警报对话框,我使用了以下代码:

                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,没有长按监听选项。我也需要一个长按监听器,以便用户可以从我提供的列表中删除一个条目(实际上仅由用户创建)。如何做到这一点?

【问题讨论】:

标签: android listview android-alertdialog


【解决方案1】:
            // 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 studio 无法解析setOnLongClickListener 方法。我还需要在单个元素上设置监听器,而不是对话框本身。
  • @AdityaChawla 提出了答案
【解决方案2】:

您需要使用 ListView 制作自己的 DialogFragment,但最好的是 RecyclerView。

例子:

public class MyDialogFragment extends DialogFragment {
    private RecyclerView mRecyclerView;
    private MyRecyclerAdapter adapter;
    // this method create view for your Dialog
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          //inflate layout with recycler view
         View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //setadapter
        CustomAdapter adapter = new MyRecyclerAdapter(context, customList);
            mRecyclerView.setAdapter(adapter);
         //get your recycler view and populate it.
         return v;
    }
}

这里你可以找到如何创建 RecyclerView 适配器,http://www.androidhive.info/2016/01/android-working-with-recycler-view/

你可以这样展示这个片段:

MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "dialogFragment");

【讨论】:

    【解决方案3】:

    在构建对话框之后,在​​显示它之前,你可以这样做:

    alertDialog.getListView().setOnLongClickListener(...);
    

    https://developer.android.com/reference/android/app/AlertDialog.html#getListView()

    编辑:添加更多代码以澄清操作

    这是添加监听器的代码。为了从列表中获取正确的对象,您应该将对象本身作为标签添加到视图中。

    //[your code from above here...]
    
            AlertDialog alertDialog = builderSingle.create();
            alertDialog.getListView().setOnLongClickListener(new OnLongClickListener() {
    
                @Override
                public boolean onLongClick(View v) {
                    //your removeing code here
                    YourObject yObj = (YourObject) v.getTag();
                    yourList.renmove(yObj);
                    return true;
                }
            });
            alertDialog.show();
    

    【讨论】:

    • 这里我认为是 AlertDialog 中的大写 A,但是无论我尝试使用您提到的这种方法,我都无法使用 getListView,您能否说明如何使用此方法?谢谢。
    • 我想在每个单独的元素上设置长按监听器,以便为用户而不是对话框本身提供删除选项。
    • @AdityaChawla a 不是大写字母,因为我把它写成一个变量。这将是 AlertDialog alertDialog = builderSingle..build();
    • @AdityaChawla 您在侦听器内部执行此操作。您会获得对单击项目的引用,因此您可以将其删除。例如,您可以显示另一个对话框以确认删除,并在 positivebutton 上从列表中删除该项目。你需要一些关于它的sn-p吗?
    • 非常感谢,Surya 发布了它。
    【解决方案4】:

    添加项目长按处理程序的简单方法

    AlertDialog dialog = new Builder(mContext)
        .setTitle(HISTORY_LIST_DIALOG_TITLE)
        .setAdapter(historyAdapter, (dialog, index) -> {
           // OnItemClickListener
        })
        .create();
    
    // Long click listener must be added after builder creation
    dialog.getListView().setOnItemLongClickListener((parent, view, index, id) -> {
      // Handle item long click here
      dialog.dismiss(); // Not required, but recommended
      return true;
    });
    
    dialog.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-29
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多