Divya Teja
Divya Teja

Reputation: 11

Intent passing from adapter to activity

Sorry for unclear question. I am new to android and stackover flow

I have an adapter which have list of images and loads from server. on Onclick of image i need to open another activity which shows full image of clicked item. at that time i need to pass image url to next activity as intent. I need help in passing intent as well as please clarify me on Is it safe to pass image url in intent from adapter to activity? Thanks for the help in advance

my adapter class

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
    List<Upload> list_of_messages;
    Context context;
    Activity mActivity;
    PhotoViewAttacher pAttacher;

    public MyAdapter(Activity activity,List<Upload> list_of_messages,Context context) {
        this.mActivity = activity;
        this.list_of_messages = list_of_messages;
        this.context=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_promise_list,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyAdapter.ViewHolder holder, final int position) {
        final Upload upload = list_of_messages.get(position);
        holder.text_date.setText(upload.getName());
        Glide.with(context).load(upload.getUrl()).into(holder.text_promise);

        final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        holder.text_promise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mActivity,ImageViewZoom.class);
                intent.putExtra("image_url",upload.getUrl());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list_of_messages.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text_date;
        ImageView text_promise;
        public ViewHolder(View itemView) {
            super(itemView);
            text_promise=itemView.findViewById(R.id.text_promise_text);
            text_date=itemView.findViewById(R.id.text_promise_date);
        }
    }

    public void onImageClicked(Activity a, String url) {
        a.startActivity(new Intent(a, FullScreenImageActivity.class).putExtra("image_url", url));
        a.overridePendingTransition(android.R.anim.fade_in, 1);
    }
}

here on clicking on holder.text_promise(Image view) i need to navigate to ImageViewZoom activity and there i need to load image from adapter.

There I got confused and asked for help.

Upvotes: 0

Views: 3714

Answers (1)

Sourabh Bans
Sourabh Bans

Reputation: 3134

I need help in passing intent

You can pass the URI to the ShowImage activity. In the intent you're using to start the activity:

Intent intent = new Intent(context, ShowImage.class);
intent.putExtra("URI", yourURI);
startActivity(intent);

Access that intent on next activity

String s = getIntent().getStringExtra("URI");

please clarify me on Is it safe to pass image url in intent from adapter to activity?

actually that is the best way to achieve the same.

Upvotes: 3

Related Questions