Android Resouce by ID / Change image onClick / no

2019-07-27 14:11发布

问题:

I have a problem with my imageview not changing after picking an image from an array (xml resource), but falling back into the default value.

Content of my 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);
            }

        });

    }    
}

Content of my 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>

Content of my 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>

Expected behaviour:

  • I click on the button id: "button_like", the image "starttest" (as declared in activity_measurement.xml) changes to a randomly picked image from the array "images_primes" (11 imtems to choose).

Actual happening:

  • I click on the button id: "button_like", the image "starttest" changes to the default color ("R.color.colorPrimaryDark").
  • The log-output seems ok, it every time I use the button it shows an other (random) number
  • Changing the command just to change the image without picking it from a xml resource (imageViewMeasurement.setImageResource(R.drawable.p13)); works without any problems

Question: Why do the images not show up (or the imageview doesn't refresh (?))?

Best tigercode

回答1:

You are defining your array wrong. and because your app can't find the correct item it goes back to the default image. a drawable is a id and an ID is an integer (you have strings) try this:

<?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>

And for retreiving the drawable

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();


回答2:

                int chosenImageNumber = (int) (Math.random() * images.length());

I think the above line is the problem. You should use:

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