通过ID /更改图像的Android资源onClick /不更改imageView

时间:2017-01-08 13:15:14

标签: android android-imageview android-xml android-drawable

从image(xml资源)中挑选图像后,我的imageview没有变化,但是又回到了默认值。

我的MeasurementActivity.java的内容

#include<iostream>
using namespace std;

class Base {};
class Derived: public Base {};
int main()
{
   Derived d;
   // some other stuff
   try {
       // Some monitored code
       throw d;
   }
   catch(Base b) { 
        cout<<"Caught Base Exception";
   }
   catch(Derived d) {  //This catch block is NEVER executed
        cout<<"Caught Derived Exception";
   }
   getchar();
   return 0;
}

我的array_images_measurement.xml的内容:

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

        });

    }    
}

我的activity_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>

预期行为:

  • 我点击按钮id:“button_like”,图像“starttest”(在activity_measurement.xml中声明)变为从“images_primes”数组中随机挑选的图像(11个模式可供选择)。

实际发生:

  • 我点击按钮ID:“button_like”,图像“starttest”变为默认颜色(“R.color.colorPrimaryDark”)。
  • 日志输出似乎没问题,每次我使用按钮时都会显示另一个(随机)数字
  • 更改命令只是为了更改图像而不从xml资源中选择它(imageViewMeasurement.setImageResource(R.drawable.p13));工作没有任何问题

问题:为什么图像没有显示(或者图像视图没有刷新(?))?

最佳

tigercode

2 个答案:

答案 0 :(得分:1)

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

Object { brand: Object, model: Object, fueltype: Object, bodystyle: Object, topspeed: Object, power: Object }

并且用于检索可绘制的

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

答案 1 :(得分:1)

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

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

float: right;