我想将MapFragment中的位置信息发送到MyDialog片段。因为我要存储在MyDialog上的数据库中。
我尝试了捆绑包,但我做不到。
public class MapFragment extends Fragment implements OnMapReadyCallback {
private static final String TAG = "MapFragment";
public Double lat, lon;
GoogleMap map;
boolean isAdded = true, click;
FloatingActionButton but;
Dialog dialog;
public MapFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
but = view.findViewById(R.id.showDialog);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdded) {
click = true;
isAdded = false;
} else {
showDialog();
isAdded = true;
}
}
private void showDialog() {
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(MapFragment.this, 1);
dialog.show(getFragmentManager(), "MyDialog");
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(final GoogleMap googleMap) {
map = googleMap;
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
public void onMapClick(LatLng latLng) {
if (click == true) {
lat = latLng.latitude;
lon = latLng.longitude;
// new LatLng(lat, lon)
Log.i("on clicked location", "loc: " + new LatLng(lat, lon));
LatLng loc = new LatLng(lat, lon);
// Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title("location"));
sendLocation(googleMap);
but.setImageDrawable(ActivityCompat.getDrawable(getContext(), R.drawable.ic_check));
//sending location mydialog fragment
MyDialog fragment = new MyDialog();
Bundle b = new Bundle();
b.putDouble("Lat", lat);
b.putDouble("Lon", lon);
fragment.setArguments(b);
}
}
});
}
public void sendLocation(final GoogleMap googleMap){
map = googleMap;
LatLng location= new LatLng(lat, lon);
Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title("location"));
}
}
MyDialog片段:
public class MyDialog extends DialogFragment
implements AdapterView.OnItemSelectedListener {
private static final String TAG = "MyDialog";
private Spinner spinner;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public interface OnInputSelected {
void sendInput(String input);
}
public OnInputSelected mOnInputSelected;
//widgets
private EditText mInput;
private Button mSave, mCancel;
private EditText nameSurname, address;
private Location location;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_map, container, false);
spinner = view.findViewById(R.id.addType);
ArrayAdapter<String> aa = new ArrayAdapter<>(getActivity(),
R.layout.support_simple_spinner_dropdown_item,
getResources().getStringArray(R.array.addType));
aa.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(aa);
spinner.setOnItemSelectedListener(this);
mSave = view.findViewById(R.id.save_action);
mCancel = view.findViewById(R.id.cancel_action);
nameSurname = view.findViewById(R.id.nameSurname);
address = view.findViewById(R.id.address);
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: closing dialog");
getDialog().dismiss();
}
});
mSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: capturing input.");
String nameSurname_ = nameSurname.getText().toString();
String address_ = address.getText().toString();
String addType_ = (String) spinner.getSelectedItem();
Dialog dialog = new Dialog();
String input = nameSurname_ + " " + address_ + " " + addType_;
if (!input.equals("")) {
dialog.setName_surname(nameSurname_);
dialog.setAddress(address_);
dialog.setAddress_type(addType_);
// dialog.setLocation(location);
MyDB.getInstance(getContext()).myDAO().addDialog(dialog);
Toast.makeText(getActivity(), "Dialog created. with " + nameSurname_ + " " + address_ + " " + addType_ , Toast.LENGTH_SHORT).show();
}
nameSurname.setText("");
address.setText("");
getDialog().dismiss();
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnInputSelected = (OnInputSelected) getTargetFragment();
} catch (ClassCastException e) {
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage());
}
}
}