简体   繁体   中英

Display images through array in Android Studio

I'm trying to re-rig a quiz app to instead of displaying text questions, will display images (Ishihara slides to be specific). For reference, here's a screenshot of the original quiz app: 原谅丑陋的黄色

When a user selects the true or false buttons, a toast appears with the result. Straightforward, and the previous/next buttons cycle through the questions. The questions are being stored as such:

final QuestionAndAnswer[] mQandA = new QuestionAndAnswer[]{
 /*1*/  new QuestionAndAnswer(R.string.question_northAmerica, true),
 /*2*/  new QuestionAndAnswer(R.string.question_antarctica, false),
 /*3*/  new QuestionAndAnswer(R.string.question_canada, false),
 /*4*/  new QuestionAndAnswer(R.string.question_madagascar, true),
 /*5*/ new QuestionAndAnswer(R.string.question_wonders, false)
};

I thought that pointing the new QuestionAndAnswer at R.drawable.imagename could accomplish this, but instead just displays the image's location as text. And changing it to new ImageView(R.drawable.imagename, true) throws the error:

ImageView (android.content.Context, android.util.AttributeSet)in ImageView cannot be applied to (int,boolean)

I'm sorry, I'm still very new to Android but as you've probably put together by now, I'm a student and don't have a choice but to do this.

If you have something like this on layout xml

<RelativeLayout ...
...
      <TextView android:id="@+id/text_view_id" ... />

...
</RelativeLayout>

You need to change it to

<RelativeLayout ...
...
      <ImageView android:id="@+id/image_view_id" ... />

...
</RelativeLayout>

Then on your activity class, maybe you have something like below

TextView textView;

public void onCreate() {

    textView = (TextView) findViewById(R.id.text_view_id);

    ....

    textView.setText(mQandA[i].question)
}

Where mQandA[i].question is your question resource aka R.string.question_northAmerica Change it to

ImageView imageView;

public void onCreate() {

     imageView = (ImageView) findViewById(R.id.image_view_id);

     ...

     imageView.setImageResource(mQandA[i].question)

}

Where mQandA[i].question is your image resource aka R.drawable.imagename

EDIT 1

change every appearance of

textView.setText(mQandA[i].question)

with

imageView.setImageResource(mQandA[i].question)

counterpart

You would have to display the questions in an ImageView.

This means that you can't just point it to a drawable, you have to edit the XML layout files, and change much of the other parts of the program.
This would include the actual code to display the questions.

You could edit your question and include the actual Java and XML files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM