Flavio Luiz Flavio Luiz - 1 month ago 7
Android Question

onActivityResult dosent works in fragment tab

i have a fragment tab using a listView. Each row of listview (arrayAdapter) have a picture button, and i set button click event like this:

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());
}


ActivityResult in my tabFragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {


ACTION _actionOrigin = TabVehicleReservationDocumentFragment._actionOrigin;

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

}


After taking de photo, i need to go to onActivityResult, but it dosent work, the event is not called. Any one can helpme? Thanks!!!

Answer Source

When you set your adapter then do something like this hope this will help you.

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;
    }
}