问题
I tried to open gallery from my adapter.
emp_photo_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((EmployeeActivity)context).startActivityForResult(i, 2017);
}
});
Then i want to show that choosen image into my imageview in my recycycleview, how to do that? Because i cant add onActivityResulton my adapter. Thanks in advance
Edit
My Full Code
public static class MyViewHolder extends RecyclerView.ViewHolder {
....
public MyViewHolder(View v) {
super(v);
....
}
public void bind(final Employee item, final OnItemClickListener listener, final Context context) {
....
generateDialog(item,dialog_employee);
....
}
...
...
void generateDialog(Employee item, View v){
//Dialog child
//Photo
emp_photo_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((EmployeeActivity)context).startActivityForResult(i, 2017);
}
});
....
}
}
回答1:
Your result will arrive in EmployeeActivity in onActivityResult. Since you are picking an image, the result will be a Uri which you will have to retrieve first, then bind to the appropriate item. I suggest the following sequence of actions:
- Create a new request code which stores the item position and identifies the request. If there are no other requests, you can simply make your row id your request code.
- Get the
Uriusingdata.getData()and remember your received request code. Make sure that the result code isActivity.RESULT_OK. - Feed the
Uriand the request code (which contains the item id) to aLoaderor something similar to retrieve the image. - Store the resulting image somewhere accessible for your
MyViewHolderfor the item id. For example, you can create aMapinside it which will store loaded images. - Find the position for the item id in the adapter and call
notifyItemChangedon the adapter for the position received. You can call eithernotifyItemChanged(int position)to do a full rebind, ornotifyItemChanged(int position, Object payload)wherepayloadwill be your bitmap.
来源:https://stackoverflow.com/questions/40482188/onactivityresult-in-recyclerview-adaptermyadapter-myviewholder