Reputation: 227
I'm working on a self-given project and I'm trying to make it so that I can call from a certain fragment my Dispenser Class in which I've created my PlacePicker method. The PlacePicker opens nicely and everything, but when I close it, it doesn't return any information that I've selected (aka the place that I've picked). I've been trying to figure this out by looking at more than ten dozen posts on the internet, including StackOverflow, and I haven't been able to find a solution that works for me.
This is the PlaceholderFragment Class that stores the fragments
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;
}
}
This is the Dispenser Class
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+"_");
}
}
Any and all suggestions are appreciated, thank you everyone in advance for helping!
Upvotes: 0
Views: 29
Reputation: 10009
You need to move the onActivityResult()
method to the activity class from which you call startActivityForResult()
(in your case the class of the localActivity
variable).
Upvotes: 1