ktina ktina - 27 days ago 7
Android Question

How to send Image from recycler view to deatil activity using picasso in Android

In my app I have got image from rest api json data, which I have shown in recyclerview. Now I would like to send the image to detail activity to show this image as Cover Image. Hence I tried with the following code to do that. But I think I am making mistake to pass the correct value of image. It would be really helpful for me if someone help me to identify the problem.

This Problem is solved

My model class is

public class NewsModel {

@Expose
private String _id;

@Expose
private String body;


@Expose
private String title;


@Expose
private List<AppImage> appImages;

@Expose
private TeaserImageSmall teaserImageSmall;

//getter and setter
}


TeaserIMageSmall Class

public class TeaserImageSmall {
@Expose
private String alt;

@Expose
private String src;


public String getSrc() {
return src;
}

public void setSrc(String src) {
this.src = src;
}
}


AdapterClass

@Override
public void onBindViewHolder(NewsHolder holder, int position) {
final NewsModel currentNews = mNews.get(position);
Picasso.with(holder.itemView.getContext());
Picasso.with(holder.itemView.getContext()).load(currentNews.getTeaserImageSmall().getSrc()).into( holder.newsImage );
holder.newsHeadline.setText(currentNews.getTitle());
holder.newsDate.setText(currentNews.getPostDate());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent i=new Intent(context,DetailNews.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("src",currentNews.getTeaserImageSmall().getSrc());
i.putExtra("title",currentNews.getTitle());
i.putExtra("body",currentNews.getBody());
context.startActivity(i);
}
});


Detail Activity Class

Intent intent = getIntent();

//RECEIVE DATA
Log.e("_coverImage",""+_coverImage);
String coverImage = intent.getStringExtra ("src");
String heading=intent.getExtras().getString("title");
//String newsDate=intent.getExtras().getString("date");
String description=intent.getExtras().getString("body");

//BIND DATA
Picasso.with(this).load(coverImage ).into(_coverImage);
_newsHeading.setText(heading);
_description.setText(description);

Answer Source

Your concept is not clear somewhat. You are passing String value in your Adapter and receiving integer in DeatilActivity class.

final NewsModel currentNews = mNews.get(position);
Picasso.with(holder.itemView.getContext());
Picasso.with(holder.itemView.getContext()).load(currentNews.getTeaserImageSmall().getSrc()).into( holder.newsImage ); //Image url 

holder.newsHeadline.setText(currentNews.getTitle());
holder.newsDate.setText(currentNews.getPostDate());
holder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

         Intent i=new Intent(context,DetailNews.class);
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         i.putExtra("src",currentNews.getTeaserImageSmall().getSrc());
         i.putExtra("title",currentNews.getTitle());
         i.putExtra("body",currentNews.getBody());
         context.startActivity(i);
     }
 });

DeatilActivity.java

String coverImage = intent.getExtras().getString("src");
    String heading=intent.getExtras().getString("title");
    //String newsDate=intent.getExtras().getString("date");
    String description=intent.getExtras().getString("body");

//BIND DATA
//_coverImage.setImageResource(coverImage);   // Remove this line
Picasso.with(DetailActivity.this).load(coverImage ).into(_coverImage);