onActivityResult in RecyclerView.Adapter<MyAdapter.MyViewHolder>

£可爱£侵袭症+ 提交于 2019-12-06 15:24:33

问题


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:

  1. 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.
  2. Get the Uri using data.getData() and remember your received request code. Make sure that the result code is Activity.RESULT_OK.
  3. Feed the Uri and the request code (which contains the item id) to a Loader or something similar to retrieve the image.
  4. Store the resulting image somewhere accessible for your MyViewHolder for the item id. For example, you can create a Map inside it which will store loaded images.
  5. Find the position for the item id in the adapter and call notifyItemChanged on the adapter for the position received. You can call either notifyItemChanged(int position) to do a full rebind, or notifyItemChanged(int position, Object payload) where payload will be your bitmap.


来源:https://stackoverflow.com/questions/40482188/onactivityresult-in-recyclerview-adaptermyadapter-myviewholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!