Return a pre-built dialog in the onActivityResult method

Asked

Viewed 160 times

2

I’m developing an android app, and I need to allow the user to upload a profile photo. The form to create the profile is in a dialog, I have the code that loads the profile photo, but when loading does not return to the dialog, returns to Activity. I would like to upload the photo and put in an imageView that I have in the dialog.

Here is my code:

 ivperfil.setOnClickListener(new View.OnClickListener() {                       
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
        Intent i = new Intent(
        Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        final int ACTIVITY_SELECT_IMAGE = 1234;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
    }
});

And in the way onActivityResult i do:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) { 
    case 1234:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap image = BitmapFactory.decodeFile(filePath);
            /* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */
            ivperfil.setImageBitmap(image);
            alertD = alertDialogBuilder.create();
            alertD.show();
        }
    }

};

But without ever returning to the dialog, returns to Activity.

1 answer

2

You can follow the following code as example of changing an item name, and only make the appropriate changes to your case(Obs: item can be a created class can you or your image in your case):

Creation of a custom Dialog:

public class EditNome extends DialogFragment implements
    DialogInterface.OnClickListener {

private ItemListener listener;
private EditText editNome;
private Item item;

@Override
public void onAttach(Activity activity) {

    super.onAttach(activity);
    if (!(activity instanceof ItemListener)) {

        throw new IllegalArgumentException(
                "A activity deve implementar EditNome");
    }
    this.listener = (ItemListener) activity;
}

public void setItem(Item item) {

    this.item = item;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Editar Nome");
    builder.setPositiveButton("OK", this);
    builder.setNegativeButton("Cancelar", this);
    LayoutInflater inflater = (LayoutInflater) getActivity()
            .getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog, null);
    builder.setView(view);
    this.editNome = (EditText) view.findViewById(R.id.editNome);
    if (item != null) {

        editNome.setText(item.getNome());
    }
    return builder.create();
}

@Override
public void onClick(DialogInterface dialog, int which) {

    if (which == DialogInterface.BUTTON_POSITIVE) {

        String name = editNome.getText().toString();
        if (!TextUtils.isEmpty(name)) {

            listener.changeNome(name);
        }
    }
}


}

Then create an Itemlistener interface:

public interface ItemListener {

public void changeNome(String nome);
}

You can call the dialog like this:

EditNome dialog = new EditNome();
dialog.setItem(item);
dialog.show(getFragmentManager(), "Dialog");

Then on your Activity you then implement the Itemlistener:

@Override
public void changeNome(String nome) {

    //faz algo
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.