Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android Studio中静态添加ImageView_Java_Android_Static_Android Resources - Fatal编程技术网

Java 在Android Studio中静态添加ImageView

Java 在Android Studio中静态添加ImageView,java,android,static,android-resources,Java,Android,Static,Android Resources,我想从android studio中的变量更改图像视图的资源。 让我解释一下,我有第一个活动“selector activity”,它有21个图像,在每次图像点击时,一个变量god_名称被更改为特定的god,现在我使用putExtra发送该变量,现在我想根据该变量更改MainActivity中ImageView的资源,例如,如果变量为“2”,然后我想将资源更改为“R.drawable.two”。 诸如此类,我在python方面有一些经验,所以我在那里使用了f字符串,这里我可以做些什么吗 主课 (

我想从android studio中的变量更改图像视图的资源。 让我解释一下,我有第一个活动“selector activity”,它有21个图像,在每次图像点击时,一个变量god_名称被更改为特定的god,现在我使用putExtra发送该变量,现在我想根据该变量更改MainActivity中ImageView的资源,例如,如果变量为“2”,然后我想将资源更改为“R.drawable.two”。 诸如此类,我在python方面有一些经验,所以我在那里使用了f字符串,这里我可以做些什么吗

主课 (注意:我没有包含导入,因此允许上载代码。)

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/god"
        android:layout_width="413dp"
        android:layout_height="496dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/hanuman" />
</androidx.constraintlayout.widget.ConstraintLayout>

我可以使用多个if条件和switch case语句,但这太大了,因为我需要写21次,所以非常困难!
提前感谢任何人的帮助

听起来您需要使用
resources.getIdentifier()
来实现这一点

我曾经写过一个相关话题的答案:

无论如何,您需要的是传递资源的名称,然后解析标识符以使用它。比如说:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // make sure that god_name is the name of the resource, so AFTER R.mipmap.[resourceName]
    // so for R.mipmap.two you need to pass "two" as "chosen_god" extra
    String god_name = getIntent().getExtras().getString("chosen_god").toString();

    ImageView god = findViewById(R.id.god);
    int resId = getResources().getIdentifier(god_name, "mipmap", getPackageName());
    god.setImageResource(resId);
}

谢谢,成功了!,我之前也尝试过同样的方法,但结果是返回0,从明天晚上开始我就很生气,为什么它不起作用?但再次感谢:-)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // make sure that god_name is the name of the resource, so AFTER R.mipmap.[resourceName]
    // so for R.mipmap.two you need to pass "two" as "chosen_god" extra
    String god_name = getIntent().getExtras().getString("chosen_god").toString();

    ImageView god = findViewById(R.id.god);
    int resId = getResources().getIdentifier(god_name, "mipmap", getPackageName());
    god.setImageResource(resId);
}