Я работаю над собственным проектом и пытаюсь сделать так, чтобы я мог вызвать из определенного фрагмента мой класс диспенсеров, в котором я создал свой метод PlacePicker. PlacePicker открывается красиво и все, но когда я закрываю его, он не возвращает никакой информации, которую я выбрал (то есть место, которое я выбрал). Я пытался выяснить это, просматривая более десяти дюжин постов в Интернете, включая StackOverflow, и мне не удалось найти решение, которое подходит мне.
Это класс PlaceholderFragment, в котором хранятся фрагменты
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class PlaceholderFragment extends Fragment {
private static final String TAG = "PlaceholderFragment";
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
if (getArguments().getInt(ARG_SECTION_NUMBER)==1){
rootView = inflater.inflate(R.layout.fragment_createevent_page1, container, false);
EditText eventName =(EditText) rootView.findViewById(R.id.editEventName);
EditText eventOrganiser =(EditText) rootView.findViewById(R.id.editOrganiser);
EditText eventDescription =(EditText) rootView.findViewById(R.id.editTextDescriptionOfEvent);
}
else if(getArguments().getInt(ARG_SECTION_NUMBER)==2){
rootView = inflater.inflate(R.layout.fragment_createevent_page2, container, false);
TextView createEventFirstDay = (TextView) rootView.findViewById(R.id.textViewFirstDay);
TextView createEventLastDay = (TextView) rootView.findViewById(R.id.textViewLastDay);
TextView createEventLocation = (TextView) rootView.findViewById(R.id.textViewLocation);
TextView createEventImageView = (TextView) rootView.findViewById(R.id.textViewEventImage);
ImageView eventImageView =(ImageView) rootView.findViewById(R.id.imageViewEventImage);
createEventFirstDay.setOnClickListener(new MyDatePickerDialog(getContext(),createEventFirstDay,"First day: "));
createEventLastDay.setOnClickListener(new MyDatePickerDialog(getContext(),createEventLastDay, "Last day: "));
createEventLocation.setOnClickListener(new Dispenser(getActivity(),createEventLocation,getContext(),101));
createEventImageView.setOnClickListener(new Dispenser(getActivity(), createEventImageView,eventImageView,102));
//textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
}
return rootView;
}
}
Это класс дозатора
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.ui.PlacePicker;
public class Dispenser implements View.OnClickListener{
private static final String TAG = "Dispenser";
private Activity localActivity;
private Context localContext;
private TextView localTextView;
private ImageView localImageView;
private int local_requested_code;
private static final int PLACE_PICKER_REQUEST = 101;
private static final int CHOOSE_IMAGE_REQUEST = 102;
public Dispenser(Activity activity, TextView textView, ImageView imageView, int requested_code){ //Dispenser for finding an image from the phone storage
this.localActivity = activity;
this.localTextView = textView;
this.localImageView = imageView;
this.local_requested_code = requested_code;
}
public Dispenser(Activity activity, TextView textView, Context context, int requested_code){ //Dispenser for opening maps and finding required location
this.localActivity = activity;
this.localTextView = textView;
this.localContext = context;
this.local_requested_code = requested_code;
}
@Override
public void onClick(View view) {
if(local_requested_code==PLACE_PICKER_REQUEST){ //This is where we start the location process..
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Intent intent;
try {
intent = builder.build(localActivity);
intent.setFlags(0);
localActivity.startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
else if(local_requested_code==CHOOSE_IMAGE_REQUEST){ //This is where we start the image finding process..
Toast.makeText(localActivity, "Choosing an image from storage", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
localActivity.startActivityForResult(Intent.createChooser(intent,"Select Event Image"),CHOOSE_IMAGE_REQUEST);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==PLACE_PICKER_REQUEST){
if(resultCode==localActivity.RESULT_OK){
Place place = PlacePicker.getPlace(localContext,data);
String locationAddress = String.format("Location address: %s",place.getAddress());
String locationName = String.format("Location name: %s",place.getName());
localTextView.setText(locationName+" | "+locationAddress);
}
}
if(requestCode == CHOOSE_IMAGE_REQUEST && resultCode == localActivity.RESULT_OK && data != null && data.getData() != null){
Uri uriEventImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(localActivity.getContentResolver(),uriEventImage);
localImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void uploadImageToFirebaseStorage() {
// StorageReference eventImageReference = FirebaseStorage.getInstance().getReference("events/"+eventName+"_"+eventOrganiser+"_");
}
}
Любые и все предложения приветствуются, спасибо всем заранее за помощь!