首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >希望在同时具有onClick侦听器和onLongClickListener的警报对话框中具有列表视图

希望在同时具有onClick侦听器和onLongClickListener的警报对话框中具有列表视图
EN

Stack Overflow用户
提问于 2016-07-05 20:03:12
回答 4查看 1.4K关注 0票数 1

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

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-07-05 20:20:27

代码语言:javascript
复制
            // 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();
票数 1
EN

Stack Overflow用户

发布于 2016-07-05 20:14:49

在构建对话框之后,在显示它之前,您可以执行以下操作:

代码语言:javascript
复制
alertDialog.getListView().setOnLongClickListener(...);

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

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

以下是添加侦听器的代码。为了从列表中获得正确的对象,您应该将对象本身作为标记添加到视图中。

代码语言:javascript
复制
//[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();
票数 0
EN

Stack Overflow用户

发布于 2016-07-05 20:18:26

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

示例:

代码语言:javascript
复制
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/

您可以像这样显示此片段:

代码语言:javascript
复制
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "dialogFragment");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38202987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档