尝试在外部活动(片段)中返回PlacePicker信息时未调用onActivityResult

时间:2018-05-14 01:11:53

标签: android performance android-fragments

我正在开发一个自己给定的项目,我正在尝试制作它,以便我可以从我的Dispenser Class中调用某个片段,我已经在其中创建了我的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;
    }

}

这是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+"_");
    }


}

感谢所有建议,感谢大家提前帮助!

1 个答案:

答案 0 :(得分:1)

您需要将onActivityResult()方法移至您调用startActivityForResult()的活动类(在您的情况下是localActivity变量的类)。