android - 无法在新 Activity 中打开图像

标签 android imageview android-imageview

需要帮助。这可能很奇怪。

第一个 Activity 有 ListView (如 lazyadapter),一旦我单击列表,它就会打开一个带有描述、标题和图像的新 Activity 。但是我无法显示图像。不确定如何发送...

public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String description = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String pdate = ((TextView)view.findViewById(R.id.pdate)).getText().toString();
//ImageView thumb = ((ImageView) view.findViewById(R.id.list_image));

// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    in.putExtra(KEY_PLAYER, title);
    in.putExtra(KEY_THUMB_URL, R.id.list_image);
    in.putExtra(KEY_TRANSACTION, description);
    in.putExtra(KEY_PUBDATE, pdate);
    startActivity(in);  }

我的第二个 Activity

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent in = getIntent();

    String title = in.getStringExtra(KEY_PLAYER);
    String description = in.getStringExtra(KEY_TEXT);
    String pdate = in.getStringExtra(KEY_PUBDATE);
    String thumb_image = in.getStringExtra(KEY_THUMB_URL);

    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblDesc = (TextView) findViewById(R.id.description_label);
    TextView lblPdate = (TextView) findViewById(R.id.pubdate_label);
    ImageView thumb = (ImageView) findViewById(R.id.thumb_label);

    lblName.setText(title);
    thumb.setImageURI(Uri.parse(thumb_image));
    lblDesc.setText(description);
    lblPdate.setText(pdate);  

最佳答案

int thumb_image = in.getIntExtra(KEY_THUMB_URL);
thumb.setImageResource(thumb_image);

您正在将资源 ID(整数)传递给您的 Intent 。当您收到该 Intent 时,您可以使用 getIntExtra 提取它,这将为您提供 R.id.list_image 的值。然后您可以在 ImageView 上调用 setImageResource 来设置图像。

当您传入整数时调用 getStringExtra 将导致空字符串。

关于android - 无法在新 Activity 中打开图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10432357/

相关文章:

android - 为什么有些 ContextCompat.getExternalFilesDirs 不可访问?

java - Android 两个网络同时操作

android - 将位图设置为 ImageView onActivityResult 不显示任何内容

android - ImageView onClick 需要点击两次才能执行操作

java - 如何使用十六进制颜色值更改按钮文本颜色?

android - 解析颜色而不是资源 ID

Android ImageView 的可绘制色调不起作用

android - ScrollView 位于另一个 Android 布局之上

java - android:layout_alignParentTop ="true"和 android:layout_centerVertical ="true"不设置 imageView 在屏幕顶部居中

android - 全屏显示图像 - 失去质量安卓