RecyclerView.Adapter< MyAdapter.MyViewHolder>中的onActivityResult. [英] onActivityResult in RecyclerView.Adapter<MyAdapter.MyViewHolder>

查看:389
本文介绍了RecyclerView.Adapter< MyAdapter.MyViewHolder>中的onActivityResult.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过适配器打开图库.

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

然后我想将选择的图像显示到我的recycycleview中的imageview中,该怎么做?因为我无法在adapter上添加onActivityResult.预先感谢

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

编辑

我的完整代码

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

推荐答案

您的结果将到达onActivityResult中的EmployeeActivity.由于您正在选择图像,因此结果将是Uri,您必须先检索Uri,然后绑定到适当的项目.我建议采取以下行动顺序:

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. 创建一个新的请求代码,该代码用于存储项目位置并标识请求.如果没有其他请求,则只需将行ID设置为请求代码.
  2. 使用data.getData()获取Uri,并记住收到的请求代码.确保结果代码为Activity.RESULT_OK.
  3. Uri和请求代码(包含商品ID)馈送到Loader或类似的位置以检索图像.
  4. 将生成的图像存储在您的MyViewHolder可访问的位置,以获取项目ID.例如,您可以在其中创建一个Map来存储加载的图像.
  5. 在适配器中找到项目ID的位置,并在适配器上调用notifyItemChanged以获取接收到的位置.您可以调用notifyItemChanged(int position)进行完全重新绑定,也可以调用notifyItemChanged(int position, Object payload),其中payload将是您的位图.
  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.

这篇关于RecyclerView.Adapter< MyAdapter.MyViewHolder>中的onActivityResult.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆