0

I'm just starting with android and I am trying to set an ImageView resource to be an id (passed from another activity) from an array.

This is the relevant part of the activity code:

Bundle extras = getIntent().getExtras(); 
int bNumber = extras.getInt("number");

Resources res = getResources();
int[] bLogos = res.getIntArray(R.array.bLogos);
int cImage = bLogos[bNumber];
ImageView imageLayout = (ImageView)findViewById(R.id.image);
imageLayout.setImageResource(cImage);

This is the relevant activity layout:

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
android:paddingTop="0dp" />

And this is the array:

<array name="bLogos">
<item>@drawable/logo1</item>
<item>@drawable/logo2</item>
<item>@drawable/logo3</item>
</array>

The problem is that my setImageResource isn't working on emulation, the drawable isn't loading. And Eclipse doesn't tell me of any errors in the code.

Any help would be appreciated.

1 Answer 1

2

Add Log.e("asd", "cImage: " + cImage); to your code. You can find that cImage is always 0. Do not use xml file to store pictures' id. It doesn't work. Change your code as below:

Bundle extras = getIntent().getExtras(); 
int bNumber = extras.getInt("number");

int[] bLogos = {R.drawable.logo1, R.drawable.logo2, R.drawable.logo3};
int cImage = bLogos[bNumber];
ImageView imageLayout = (ImageView)findViewById(R.id.image);
imageLayout.setImageResource(cImage);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.