我想從我的Android應用程序的一部分中使用谷歌的placepicker。我似乎不能使用onActivityResult返回的數據。我使用(https://inthecheesefactory.com/blog/how-to-fix-nested-fragment-onactivityresult-issue/en)的解決方案將結果返回到片段中。然而,這個解決方案不允許我根據我的placePicker輸入來更改視圖中的元素。與地方信息吐司工作,但不改變按鈕,因爲它在膨脹的視圖外。我正在使用片段,因爲我的應用是帶有2個片段的選項卡式滑塊應用,可創建位置和查看位置。在片段中使用PlacePicker
請幫忙,如有需要請澄清!
這裏是onCreateView在myFragment延伸片段類:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_make_minyan, container, false);
//Map Place Activity
final Button locationButton = (Button) rootView.findViewById(R.id.button_location);
locationButton.setOnClickListener(new View.OnClickListener() {
//int RESULT_OK;
public void onClick(View v) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
getActivity().startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
//a bunch of other buttons and spinner etc that are working.
return rootView;
}
這裏是從上面鏈接的溶液中使用的onActivityResult代碼。 註釋掉的行是當前不會工作的代碼行。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Place place = PlacePicker.getPlace(data, getActivity());
//locationButton.setText(String.format("Place: %s", place.getName()));
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(getContext(), toastMsg, Toast.LENGTH_LONG).show();
}
}
}