onActivityResult在片段选项卡中不起作用

时间:2017-08-23 12:23:45

标签: android listview android-fragments android-arrayadapter android-fragmentactivity

我有一个使用listView的片段选项卡。列表视图的每一行(arrayAdapter)都有一个图片按钮,我设置按钮点击事件,如下所示:

public View getView(final int position, View convertView, ViewGroup parent) 
{
    View row = convertView;

    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.vehiclereservationdocument_list, parent, false);
    }

    VehicleReservationDocument vrDocument = getItem(position);

    Button btnTakePicture = (Button) row.findViewById(R.id.btnTakePicture);
    btnTakePicture.setTag(R.id.WOP_POSITION, position);
    btnTakePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            takePicture((Integer) view.getTag(R.id.WOP_POSITION));
        }
    });

    btnTakePicture.setVisibility(View.VISIBLE);

    return row;
}

public void takePicture(int position) {
    // Ação do botão

    documentStrategy = DocumentStrategy.PICTURE;
    VehicleReservationDocument vrd = vehicleReservationDocuments.get(position);
    this.argDocument = position;

    fileUri = getOutputMediaFileUri(vrd.getVehicleReservation());

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    VehicleReservationTabFragment.fragmentWhoCalledStartActivityForResult = VehicleReservationTabFragment.VehicleReservationTabFragmentGroupId.TAB_VEHICLERESERVATION_DOCUMENT.ordinal();
    TabVehicleReservationDocumentFragment.setFileUri(fileUri);
    TabVehicleReservationDocumentFragment.setArgDocument(argDocument);

    ((Activity) context).startActivityForResult(intent, documentStrategy.ordinal());
}

我的tabFragment中的ActivityResult:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        ACTION _actionOrigin = TabVehicleReservationDocumentFragment._actionOrigin;

        UploadDocument uploadDocument = new UploadDocument(getActivity(), _actionOrigin);
        uploadDocument.execute();
}

拍摄照片后,我需要转到onActivityResult,但它不起作用,事件未被调用。任何人都可以帮助我吗?感谢!!!

4 个答案:

答案 0 :(得分:0)

只需使用没有上下文的startActivityForResult

startActivityForResult(intent, documentStrategy.ordinal());

答案 1 :(得分:0)

在片段类中移动 takePicture 方法。并使用

SOCIAL_AUTH_GITHUB_SCOPE = ['user:email']

而不是

startActivityForResult(intent, documentStrategy.ordinal());

第一个方法回调片段onActivityResult,其中,第二个回调活动的onActivityResult

答案 2 :(得分:0)

要在片段中获得结果,请确保调用startActivityForResult(intent,111);而不是getActivity()。startActivityForResult(intent,111);在你的片段里面。

答案 3 :(得分:0)

当您设置适配器时,请执行此类操作,希望这对您有所帮助。

YourAdapter adapter = new YourAdapter(do your stuff, clickListener);
listView.setAdapter(adapter);


Define this method in your Activity

View.OnClickListener clickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        documentStrategy = DocumentStrategy.PICTURE;
        VehicleReservationDocument vrd = vehicleReservationDocuments.get(position);
        this.argDocument = position;

        fileUri = getOutputMediaFileUri(vrd.getVehicleReservation());

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        VehicleReservationTabFragment.fragmentWhoCalledStartActivityForResult = VehicleReservationTabFragment.VehicleReservationTabFragmentGroupId.TAB_VEHICLERESERVATION_DOCUMENT.ordinal();
        TabVehicleReservationDocumentFragment.setFileUri(fileUri);
        TabVehicleReservationDocumentFragment.setArgDocument(argDocument);

        startActivityForResult(intent, documentStrategy.ordinal());
    }
};

**Your Adapterclass**

public class YourAdapter extend BaseAdapter {
    private View.onClickListener clickListener;

    public YourAdapter(your stuff, View.onClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.vehiclereservationdocument_list, parent, false);
        }

        VehicleReservationDocument vrDocument = getItem(position);


        Button btnTakePicture = (Button) row.findViewById(R.id.btnTakePicture);
        btnTakePicture.setTag(R.id.WOP_POSITION, position);
        btnTakePicture.setOnClickListener(clickListener);

        btnTakePicture.setVisibility(View.VISIBLE);

        return row;
    }
}