Je développe une application de quiz simple dans lequel j'utilise des images comme des questions et aussi des images comme choix. Le problème est que lorsque j'assinging choix qui est de type ImageView à MANSWER qui est de type int Je reçois une erreur qui est:Comment puis-je appliquer Imageview à int avec un opérateur égal dans Android
« == » opérateur ne peut pas être appliqué à « android.widget .imageView ',' int '
Je sais qu'il n'est pas possible d'affecter ImageView directement à int. J'ai essayé d'obtenir la ressource d'image pour le choix mais ne peux trouver aucune méthode pour accéder directement à la ressource d'image.
public class counting extends AppCompatActivity {
ImageView choice_one;
private Questions mQuestions = new Questions();
private int mAnswer;
private int mQuestionsLength = mQuestions.mQuestions.length;
Random r;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counting);
r = new Random();
choice_one=(ImageView)findViewById(R.id.choice_1);
question=(ImageView)findViewById(R.id.question);
updateQuestions(r.nextInt(mQuestionsLength));
choice_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(choice_one == mAnswer){
mp=MediaPlayer.create(counting.this,R.raw.bird);
mp.start();
updateQuestions(r.nextInt(mQuestionsLength));
}else{
mp=MediaPlayer.create(counting.this,R.raw.april);
mp.start();
}
}
});
}
private void updateQuestions(int num){
question.setImageResource(mQuestions.getQuestion(num));
choice_one.setImageResource(mQuestions.getChoice1(num));
mAnswer = mQuestions.getAnswer(num);
}
}
Question.class
public class Questions {
public int mQuestions[]={
R.drawable.onee, R.drawable.twoe, R.drawable.threee,
R.drawable.foure, R.drawable.sixe, R.drawable.eighte
};
public int mChoices[][]={
{R.drawable.counting_one, R.drawable.counting_two, R.drawable.counting_three, R.drawable.counting_four},
{R.drawable.counting_one, R.drawable.counting_two, R.drawable.counting_three, R.drawable.counting_four},
{R.drawable.counting_one, R.drawable.counting_two, R.drawable.counting_three, R.drawable.counting_four},
{R.drawable.counting_one, R.drawable.counting_two, R.drawable.counting_three, R.drawable.counting_four},
{R.drawable.counting_one, R.drawable.counting_two, R.drawable.counting_six, R.drawable.counting_four},
{R.drawable.counting_one, R.drawable.counting_eight, R.drawable.counting_three, R.drawable.counting_four}
};
public int mAnswers[]={R.drawable.counting_one,R.drawable.counting_two,R.drawable.counting_three,
R.drawable.counting_four, R.drawable.counting_six, R.drawable.counting_eight};
public int getQuestion(int a){
int question = mQuestions[a];
return question;
}
public int getChoice1(int a){
int choice = mChoices[a][0];
return choice;
}
public int getChoice2(int a){
int choice = mChoices[a][1];
return choice;
}
public int getChoice3(int a){
int choice = mChoices[a][2];
return choice;
}
public int getChoice4(int a){
int choice = mChoices[a][3];
return choice;
}
public int getAnswer(int a){
int answer = mAnswers[a];
return answer;
}
}
Que signifierait même que 'ImageView' soit égal à' 42'? – azurefrog
Que voulez-vous dire par 42? – danish
https://www.quora.com/Why-and-how-is-42-the-answer-to-life-the-universe-and-everything – Henry