首页 > 解决方案 > How to receive data from Dialog Fragment in another fragment's setOnMapLongClickListener listener

问题描述

So this is my public View onCreateView method. I want to open dialog window when user long clicks on map, then take data from the dialog and then set .tittle according to data user inputted into the dialog. I tried doing it with interface but it would not have the data when I need it. getInput() is where I want to receive the data from Dialog fragment.

            mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
                @Override
                public void onMapLongClick(LatLng latLng) {
                    //FragmentManager fm = getActivity().getSupportFragmentManager();
                    DialogMap dial = new DialogMap();
                    dial.setTargetFragment(MapFragment.this, 1);
                    dial.show(getFragmentManager(), "Add a catch");

                        mMap.addMarker(new MarkerOptions()
                                .position(latLng)
                                .title(getinput())
                                .snippet("Your marker snippet")
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.fish_icon_map)));

enter image description here enter image description here

This is my dialog's fragment.

标签: androidgoogle-mapsandroid-fragments

解决方案


您错过了 DialogMap 中对话侦听器的初始化。

公共 onDialogOk 对话框。(你必须在使用前初始化它)

dialog = (onDialogOk)getTargetFragment()

所以在 onCreateView() 里面,你可以初始化它。


推荐阅读