gpt4 book ai didi

java - 暂停直到函数完成运行

转载 作者:行者123 更新时间:2023-11-29 21:20:38 25 4
gpt4 key购买 nike

我正在尝试编写一个在 Google map 上标记特定标记的应用。当在 map 上长按一个点时,会弹出一个对话框,用户可以在其中输入有关该点的信息。然后,此信息将显示在标记的信息窗口中。

对于我编写的代码,当长按一个点时,showEditDialog() 运行并出现对话框,但应用程序不会等待用户输入信息并按 OK,然后运行其余代码在 map 上长按。这意味着新添加的点没有此信息,因为应用程序会在保存数据之前尝试从 SharedPreferences 加载数据,但是当应用程序重新启动并重新加载所有标记时,该信息就会出现。我该如何解决这个问题?

主要 Activity :

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();

}
}

最佳答案

由于这些操作是异步的,您应该根据适当的事件触发每个阶段,在本例中是对话框的结束,因此将仅在对话框关闭后才应执行的代码放在 onFinishEditDialog1 中/onFinishEditDialog2,不是在调用 showEditDialog

之后

关于java - 暂停直到函数完成运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20781620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com