0

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);
            }
        });
        ....
    }
}
Huzain
  • 69
  • 2
  • 8

1 Answers1

0

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.
Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • how to send Uri to myviewholder? – Huzain Nov 08 '16 at 09:33
  • @Huzain I would probably have a method like `void putLoadedBitmap(int itemId, @NonNull Bitmap bitmap)` on `MyViewHolder`, and it would store the bitmap internally in a map. – Malcolm Nov 08 '16 at 09:34
  • yeah, then how to call that method? myviewholder is inside myadapter. I tried to call any method, but i dont know how to call that method in myviewholder that was inside myadapter – Huzain Nov 08 '16 at 09:37
  • @Huzain Sorry, I meant that you need to create a method on the adapter, and the view holder can hold a reference to the adapter to get the bitmaps. – Malcolm Nov 08 '16 at 09:38
  • could u please give me an example that method? – Huzain Nov 08 '16 at 09:41
  • @Huzain The method will just have a line like `itemBitmapsMap.put(itemId, bitmap)` in it, what's there to give an example for? – Malcolm Nov 08 '16 at 09:43
  • how to get reference in view holder to my adapter when i get call some method in adapter? – Huzain Nov 08 '16 at 09:48
  • @Huzain Just get it in constructor and store it in a field (`MyViewHolder(View view, MyAdapter adapter)`), inside you call `this.adapter = adapter`. – Malcolm Nov 08 '16 at 09:52
  • i still didnt get it, sorry :( – Huzain Nov 08 '16 at 10:04
  • @Huzain I'm afraid you'll have to brush up on Java basics in that case, I can't write the code for you. – Malcolm Nov 08 '16 at 10:04
  • i know what r u trying to tell me, but actually what i dont know is how to call method in viewholder from activity that contain activity result? – Huzain Nov 08 '16 at 10:55
  • @Huzain You don't need to call anything in the view holder, you just invalidate the adapter, and the view holders will do the binding again, this time with the loaded bitmap. I hope you can access the adapter from the activity. – Malcolm Nov 08 '16 at 10:57
  • @Huzain That's what step 5 does. – Malcolm Nov 08 '16 at 11:03