简体   繁体   English

从另一个活动设置ImageView资源和TextView文本

[英]Set ImageView Resource and TextView Text from Another Activity

I have a list view in an activity and when a row is selected I want to show another activity that contains an ImageView and a TextView. 我在一个活动中有一个列表视图,当选择一行时,我想显示另一个包含ImageView和TextView的活动。 How can I set the image and text from the first activity? 如何在第一个活动中设置图像和文本? I'm guessing for the text I can use an extra with the intent. 我猜想我可以在文字上加上一些文字。

intent.putExtra("data", bitmap)
intent.putExtra("your_string", strName);

You can recieve bitmap from another activity like this 您可以像这样从其他活动中接收位图

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("data");

It costs a lot of memory and is also slow. 它占用大量内存,而且速度很慢。

If you want to pass it inbetween activities, you can store it in a file. 如果要在活动之间传递它,可以将其存储在文件中。 That's more efficient. 这样更有效。

There are multiple ways to sending data between Activities 活动之间有多种发送数据的方法

  1. you can send image intent.putExtra("data", bitmap) and recieve it as @deniz suggested 您可以发送图像intent.putExtra("data", bitmap)并按照@deniz的建议进行接收
  2. you can use Serializable to send data 您可以使用Serializable发送数据
  3. you can create Parcelable and send it to next Activity 您可以创建Parcelable并将其发送到下一个活动
  4. create a static Bitmap and get it in another Activity but it is not a good approach and against OOP 创建一个静态位图并在另一个Activity中获取它,但这不是一个好方法,并且反对OOP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM