android - 图像未使用 Intent 发送到另一个活动
问题描述
我正在尝试将用户帖子从适配器发送到另一个活动。我是这样做的:
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Post post= mPost.get(position);
Glide.with(mContext).load(post.getPostimage())
.apply(new RequestOptions().placeholder(R.drawable.loading_image))
.into(holder.post_image);
holder.post_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, PostDetails.class);
i.putExtra("postimage",post.getPostimage());
mContext.startActivity(i);
((Activity) mContext).overridePendingTransition(0, 0);
}
});
}
在其他活动中:
postImage = findViewById(R.id.postImage);
postImage.setImageResource(getIntent().getIntExtra("postimage", 0));
这是 Post 模型类:
public class Post {
private String postid;
private String postimage;
private String title;
private String publisher;
public Post(String postid, String postimage, String title, String publisher)
{
this.postid = postid;
this.postimage = postimage;
this.title = title;
}
public Post ()
{ }
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPostimage() {
return postimage;
}
public void setPostimage(String postimage) {
this.postimage = postimage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
一切看起来都很好,但是第二个活动中没有显示图像。现在有错误消息!我不知道问题是什么。
解决方案
postImage.setImageResource(getIntent().getStringExtra("postimage"));
您正在将 String 值传递给图像资源,但图像资源需要 Int 。
您可以使用 glide 加载图像,因此从 String 额外方法获取 Intent 因为您必须获取字符串而不是int
Glide.with(this).load(getIntent().getStringExtra("postimage"))
.apply(new RequestOptions().placeholder(R.drawable.loading_image))
.into(postImage);
推荐阅读
- javascript - 带有函数的 onclick 不会在按钮单击时运行函数
- ios - 如何使用 Swift 启用和禁用基于 UITextView 文本长度的警报操作按钮
- css - 为什么“inline-block”会修剪元素内部末尾的空间?
- java - 使用 PageFactory 初始化 PageObjects 的不同方法
- reactjs - 当我关闭互联网连接时,它没有显示来自 NetInfo.isConnect 函数的错误值
- c# - 无法使用 cli 层在非托管 c++ 代码中编译托管 c# 代码
- python - 如何仅使用 random.randint() 随机化列表?(关闭)
- c# - 如何只处理最后一个任务并在c#中丢弃上一个任务
- jacoco - API代码覆盖率的Jacoco集成测试
- javascript - 从下拉列表中选择元素时从 json 文件加载数据