0

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: Forgive the ugly yellow

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.

2
  • 1
    Problem is "new ImageView". Your ImageView should be declared in XML, and Activity will use that by "findViewById". See @Sidhi Artha's answer. Commented Mar 14, 2017 at 2:22
  • 1
    Yep, that is what I have been doing since he posted. I didn't want to reply until I figured it out though. Commented Mar 14, 2017 at 2:26

1 Answer 1

2

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 a.k.a 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 a.k.a R.drawable.imagename

EDIT 1

change every appearance of

textView.setText(mQandA[i].question)

with

imageView.setImageResource(mQandA[i].question)

counterpart

Sign up to request clarification or add additional context in comments.

7 Comments

So I've implemented your suggestions, and while there are no errors in the code, I can only get the first image to appear. I will keep working and keep you posted if/when I find a solution. Felt like I should do an update after ~20min.
@bonzo: Maybe you can post your code, especially the Activity class, but keep in mind tough that you need to change the ImageView image when you press next or previous button as you previously do in your TextView, see my edit above
My next button uses mNextButton = (Button)findViewById(R.id.next_button); mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCurrentQuestionIndex = (mCurrentQuestionIndex + 1) % mQandA.length; updateQuestion(); Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT).show(); } }); The Toast is is just for testing of course.
An additional note, setting `imageView.setImageResource(R.drawable.image1);' is what I believe is what's causing the problem. Because whatever is in this code (in this case image1), is what gets displayed and won't cycle when next or previous is clicked.
That is absolutely correct. I changed the UpdateQuestion() method to: private void updateQuestion(){ int question = difficultySwitch[mCurrentQuestionIndex].getQuestion(); imageView = (ImageView) findViewById(R.id.image_view_id); imageView.setImageResource(question); } After I made that change, it started working perfectly. I'll mark your reply as the solution.
|

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.