Karim Abd Elhameed Mohamed Karim Abd Elhameed Mohamed - 3 months ago 56
Android Question

Android How to get image URL from recyclerView (Not Local image)

My Problem is that I have a recyclerView consists of list of images (From Flickr_API) and their text , What I want to do is When the user tap on any photo a new RecyclerView should be showed listing more photos for that particular user. So , I'm trying to catch the image url once the user clicks on it (any other solution solve the problem will be Accepted)
I've tried to set a clickListner Function on my BindViewHolder in my RecyclerView Adapter Class like this :

@Override
public void onBindViewHolder(final PhotoViewHolder holder, final int position) {
final FlickrPhoto photo = photos.get(position);
holder.textView.setText(photo.title);
uri = Uri.parse(photo.image);
Glide.with(context).load(uri).placeholder(R.drawable.image_placeholder).crossFade().into(holder.imageView);
PhotoViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context,SpecificUserPhotoRecyclerView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

}
});


My Connection service is Retrofit2 and I am using Glide Library to catch images from Flickr_API .
So , please help me How do I pass any data helps me for catching more user photos in the other class (Which my Intent refers to ) .

Note
My photos that I'm trying to catch along all my project isn't local images
because, I found many solutions but all depends on your drawable folder which will not help me in my case .

Edit
After trying to use SetTag and getTag in my onBindViewHolder Like this :

@Override
public void onBindViewHolder(final PhotoViewHolder holder, final int position) {
final FlickrPhoto photo = photos.get(position);
holder.textView.setText(photo.title);
uri = Uri.parse(photo.image);
Glide.with(context).load(uri).placeholder(R.drawable.image_placeholder).crossFade().into(holder.imageView);

PhotoViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.imageView.setTag(photo.title);
Context context = v.getContext();
Intent intent = new Intent(context,SpecificUserPhotoRecyclerView.class);
intent.putExtra("title",String.valueOf(holder.imageView.getTag()).toString());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Toast.makeText(context,holder.imageView.getTag().toString(),Toast.LENGTH_LONG).show();
context.startActivity(intent);

}
});


}


I've Got a correct Toast message with the correct imgTitle but when I've tried to receive that Intent on the other class I receive it with null value

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_specific_user_photo);
//photosListActivity = new PhotosListActivity();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.user_photo_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),1));
adapter = new PhotosRecyclerViewAdapter(getApplicationContext(),getPhotos());
recyclerView.setAdapter(adapter);
Intent intent = new Intent();
String titleImg = intent.getStringExtra("title");
Log.w("============>imgURL = "+titleImg,"***********");
Toast.makeText(this,titleImg,Toast.LENGTH_LONG).show();

}

Answer

you can set imageUrl as setTag with ImageView,

holder.imageView.setTag(photo.title);

And when user click it,

you can get the value,

holder.imageView.getTag();

Hope this will help you.

Thanks,