RecyclerView.Adapter< MyAdapter.MyViewHolder>中的onActivityResult. [英] onActivityResult in RecyclerView.Adapter<MyAdapter.MyViewHolder>
问题描述
我试图通过适配器打开图库.
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 onActivityResult
on 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:
- 创建一个新的请求代码,该代码用于存储项目位置并标识请求.如果没有其他请求,则只需将行ID设置为请求代码.
- 使用
data.getData()
获取Uri
,并记住收到的请求代码.确保结果代码为Activity.RESULT_OK
. - 将
Uri
和请求代码(包含商品ID)馈送到Loader
或类似的位置以检索图像. - 将生成的图像存储在您的
MyViewHolder
可访问的位置,以获取项目ID.例如,您可以在其中创建一个Map
来存储加载的图像. - 在适配器中找到项目ID的位置,并在适配器上调用
notifyItemChanged
以获取接收到的位置.您可以调用notifyItemChanged(int position)
进行完全重新绑定,也可以调用notifyItemChanged(int position, Object payload)
,其中payload
将是您的位图.
- 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
Uri
usingdata.getData()
and remember your received request code. Make sure that the result code isActivity.RESULT_OK
. - Feed the
Uri
and the request code (which contains the item id) to aLoader
or something similar to retrieve the image. - Store the resulting image somewhere accessible for your
MyViewHolder
for the item id. For example, you can create aMap
inside it which will store loaded images. - Find the position for the item id in the adapter and call
notifyItemChanged
on the adapter for the position received. You can call eithernotifyItemChanged(int position)
to do a full rebind, ornotifyItemChanged(int position, Object payload)
wherepayload
will be your bitmap.
这篇关于RecyclerView.Adapter< MyAdapter.MyViewHolder>中的onActivityResult.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!