首页 > 解决方案 > 图像未使用 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;
        }
    }

一切看起来都很好,但是第二个活动中没有显示图像。现在有错误消息!我不知道问题是什么。

标签: android

解决方案


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);

推荐阅读