나는 이미지를 질문으로 사용하고 객관식 퀴즈 앱을 개발 중입니다. 여기서 문제는 내가 배열에서 이미지를 비교하는 방법을 모른다는 것입니다. onclick 메서드에서 배열에 저장된 올바른 대답을 클릭 한 선택 비교하지만 해당 작동하지 심지어 setTag
및 선택에 대한 getTag
방법은 내가 무엇을 시도한 코드가 잘못되었습니다.어떻게 이미지를 비교할 수 있습니까
제발 대답 downvote 질문에 대답하고 싶지 않으면.
여기 내 코드입니다.
public class counting extends AppCompatActivity {
ImageView choice_one, choice_two, choice_three, choice_four;
ImageView question;
MediaPlayer mp;
private Questions mQuestions = new Questions();
private int mAnswer;
private int mQuestionsLength = mQuestions.mQuestions.length;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counting);
r = new Random();
choice_one=(ImageView)findViewById(R.id.choice_1);
choice_two=(ImageView)findViewById(R.id.choice_2);
choice_three=(ImageView)findViewById(R.id.choice_3);
choice_four=(ImageView)findViewById(R.id.choice_4);
choice_one.setTag(1);
choice_two.setTag(2);
choice_three.setTag(3);
choice_four.setTag(4);
question=(ImageView)findViewById(R.id.question);
updateQuestions(r.nextInt(mQuestionsLength));
choice_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getImageResource(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();
}
}
});
choice_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getImageResource(choice_two) == 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();
}
}
});
choice_three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getImageResource(choice_three) == 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();
}
}
});
choice_four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getImageResource(choice_four) == 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 int getImageResource(ImageView iv){
return (Integer)iv.getTag();
}
private void updateQuestions(int num){
question.setImageResource(mQuestions.getQuestion(num));
choice_one.setImageResource(mQuestions.getChoice1(num));
choice_two.setImageResource(mQuestions.getChoice2(num));
choice_three.setImageResource(mQuestions.getChoice3(num));
choice_four.setImageResource(mQuestions.getChoice4(num));
mAnswer = mQuestions.getAnswer(num);
}
}
질문 클래스 :
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;
}
}
이 getImageResource (choice_num)'의 값 당신이 제공 한 그 (코드 아래) 태그를 제거하고 시도 '와 'mAnswer' 당신이 기대하는 바는 무엇입니까? 이들이 모두 예상대로 작동하는지 확인하기 위해 인쇄 문을 넣으십시오. 'mAnswers []'배열을 상호 참조로 인쇄 할 수도 있습니다. –
'getImageResource'에 setTag를 사용하지만'mAnswer'에서 값을 가져 오는 법을 모르겠습니다. – danish