我正在尝试在Google地图上编写某些标记的应用。在地图上长按一个点时,会弹出一个对话框,用户输入有关该点的信息。然后,此信息将显示在标记的信息窗口中。
对于我编写的代码,当长按一个点时,showEditDialog()会运行并出现对话框,但应用程序不会等待用户输入信息并按下OK然后再运行其余的代码OnMapLongClick。这意味着新添加的点没有此信息,因为应用程序尝试在保存之前从SharedPreferences加载数据,但在重新启动应用程序并重新加载所有标记时会显示该信息。我该如何解决这个问题?
主要活动:
mMap.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
sharedPreferences = getSharedPreferences("location", 0);
int locationCount = sharedPreferences.getInt("locationCount", 0);
showEditDialog();
locationCount++;
String name = sharedPreferences.getString("name"+Integer.toString(locationCount-1), null);
String url = sharedPreferences.getString("url"+Integer.toString(locationCount-1), null);
// Drawing marker on the map
mMap.addMarker(new MarkerOptions()
.position(point)
.title(name)
.snippet(url)
);
/** Opening the editor object to write data to sharedPreferences */
SharedPreferences.Editor editor = sharedPreferences.edit();
// Storing the latitude for the i-th location
editor.putString("lat"+ Integer.toString((locationCount-1)), Double.toString(point.latitude));
// Storing the longitude for the i-th location
editor.putString("lng"+ Integer.toString((locationCount-1)), Double.toString(point.longitude));
// Storing the count of locations or marker count
editor.putInt("locationCount", locationCount);
/** Saving the values stored in the shared preferences */
editor.commit();
Toast.makeText(getBaseContext(), "New Camera Added!", Toast.LENGTH_SHORT).show();
}
});
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
AddCamDialog addCamDialog = new AddCamDialog();
addCamDialog.show(fm, "fragment_newcam");
}
@Override
public void onFinishEditDialog1(String EditText1) {
sharedPreferences = getSharedPreferences("location", 0);
int locationCount = sharedPreferences.getInt("locationCount", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name"+ Integer.toString((locationCount-1)), EditText1);
editor.commit();
}
@Override
public void onFinishEditDialog2(String EditText2) {
sharedPreferences = getSharedPreferences("location", 0);
int locationCount = sharedPreferences.getInt("locationCount", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("url"+ Integer.toString((locationCount-1)), EditText2);
editor.commit();
}
对话类:
public class AddCamDialog extends DialogFragment {
public interface AddCamDialogListener {
void onFinishEditDialog1(String EditText1);
void onFinishEditDialog2(String EditText2);
}
private EditText EditText1;
private EditText EditText2;
public AddCamDialog() {
// Empty constructor required for DialogFragment
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.title_NewCamera);
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_newcam, null);
EditText1 = (EditText) view.findViewById(R.id.cam_name);
EditText2 = (EditText) view.findViewById(R.id.cam_URL);
builder.setView(view);
builder.setPositiveButton(R.string.PositiveButton, new DialogInterface.OnClickListener() {
//@Override
public void onClick(DialogInterface dialog, int which) {
AddCamDialogListener activity = (AddCamDialogListener) getActivity();
activity.onFinishEditDialog1(EditText1.getText().toString());
activity.onFinishEditDialog2(EditText2.getText().toString());
dialog.cancel();
}
});
builder.setNegativeButton(R.string.NegativeButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}
答案 0 :(得分:2)
由于这些操作是异步的,你应该根据相应的事件触发每个阶段,在这种情况下是对话框的结束,所以只有在{{1}中关闭对话框后才应该执行代码} / onFinishEditDialog1
,不是在通话onFinishEditDialog2