Android 按 ID 获取资源/单击时更改图像/不更改 imageView

Android Resouce by ID / Change image onClick / no change of imageView

我有一个问题,我的 imageview 在从数组(xml 资源)中选取图像后没有改变,而是回到默认值。

我的 MeasurementActivity.java

的内容
public class MeasurementActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measurement);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        final ImageView imageViewMeasurement = (ImageView) findViewById(measurement_image_view);

        //Button here to change the image in the imageView "imageViewMeasurement"
        Button button_like = (Button) findViewById(R.id.button_like);
        button_like.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("MYAPP", "Like-Button clicked");

                /*imageViewMeasurement.setImageResource(R.drawable.p13);*/

                TypedArray images = getResources().obtainTypedArray(R.array.images_primes);
                int chosenImageNumber = (int) (Math.random() * images.length());

                // setImageResource to the random chosenImageNumber
                imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorPrimaryDark));
                images.recycle();

                // Confirmation if the random generator picked a Number from the array
                String chosenImageNumberTest = String.valueOf(chosenImageNumber);
                Log.d("MYAPP Choice Number", chosenImageNumberTest);
            }

        });

    }    
}

我的 array_images_measurement.xml 的内容:

<resources>
    <array name="images_primes">
            <item>"@drawable/p1"</item>
            <item>"@drawable/p15"</item>
            <item>"@drawable/p20"</item>
            <item>"@drawable/p25"</item>
            <item>"@drawable/p30"</item>
            <item>"@drawable/p35"</item>
            <item>"@drawable/p40"</item>
            <item>"@drawable/p45"</item>
            <item>"@drawable/p50"</item>
            <item>"@drawable/p55"</item>
            <item>"@drawable/p60"</item>
            <item>"@drawable/p65"</item>
    </array>
    <array name="images_target">
            <item>"@drawable/t1"</item>
            <item>"@drawable/t5"</item>
            <item>"@drawable/t10"</item>
            <item>"@drawable/t15"</item>
            <item>"@drawable/t20"</item>
    </array>

我的内容activity_measurement.xml:

<RelativeLayout 
                android:layout_width="match_parent" 
                android:layout_height="match_parent" 
                android:layout_alignParentTop="true" 
                android:layout_alignParentStart="true">

        <ImageView 
                   android:id="@+id/measurement_image_view" 
                   android:layout_width="match_parent" 
                   android:layout_height="400dp" 
                   android:layout_alignParentLeft="true" 
                   android:text="Left" 
                   android:src="@drawable/starttest" 
                   android:padding="10dp" />
        <Button 
                android:text="@string/button_like" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/button_like" 
                android:layout_marginBottom="26dp" 
                android:layout_alignParentBottom="true" 
                android:layout_alignParentStart="true" /> 
</RelativeLayout>

预期行为:

实际发生:

问题:为什么图片不显示(或者图片视图不刷新(?))?

最佳 老虎代码

您对数组的定义有误。并且由于您的应用找不到正确的项目,它会返回到默认图像。一个drawable是一个id,一个ID是一个整数(你有字符串)试试这个:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <array name="random_imgs">
        <item>@drawable/tests1</item>
        <item>@drawable/tests2</item>
        <item>@drawable/tests3</item>
    </array>
</resources>

并用于检索可绘制对象

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);

Random r = new Random();
int i = random.nextInt(imgs.size() -1);

// get resource ID by index
imgs.getResourceId(i, R.color.colorPrimaryDark)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, R.color.colorPrimaryDark));

// recycle the array
imgs.recycle();
                int chosenImageNumber = (int) (Math.random() * images.length());

我认为上面一行是问题所在。 你应该使用:

Match.random(images.length(-1));