簡體   English   中英

圖像不會使用 Intent 發送到另一個活動

[英]Image is not sent to another activity using 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));

這是后模型類:

  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 值傳遞給圖像 Resource 但圖像資源采用 Int 。

您可以使用 glide 加載圖像,因此從 String 額外方法獲取 Intent 因為您必須獲取string而不是int

Glide.with(this).load(getIntent().getStringExtra("postimage"))
                .apply(new RequestOptions().placeholder(R.drawable.loading_image))
                .into(postImage);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM