以下是我的代码 -
public class GameActivity extends Activity implements OnClickListener {
ImageView mainImage;
Button questionValue;
Button option1;
Button option2;
Button option3;
int rightAnswer;
Question question;
Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mainImage = (ImageView) findViewById(R.id.idc_questionImage);
questionValue = (Button) findViewById(R.id.idc_question);
option1 = (Button) findViewById(R.id.idc_questionOption1);
option2 = (Button) findViewById(R.id.idc_questionOption2);
option3 = (Button) findViewById(R.id.idc_questionOption3);
Intent intent = getIntent();
ArrayList<Question> questions = intent.getParcelableArrayListExtra("Question");
for (Iterator iterator = questions.iterator(); iterator.hasNext();) {
question = (Question) iterator.next();
System.out.println("ArrayList " + question);
mainImage.setBackgroundResource(question.getQuestionImage());
questionValue.setBackgroundResource(question.getQuestionTitle());
option1.setBackgroundResource(question.getOptionImage1());
option2.setBackgroundResource(question.getOptionImage2());
option3.setBackgroundResource(question.getOptionImage3());
rightAnswer = question.getRightAnswerImage();
if(question.getOptionImage1() == rightAnswer)
{
option1.setOnClickListener(this);
}
if(question.getOptionImage2() == rightAnswer)
{
option2.setOnClickListener(this);
}
if(question.getOptionImage3() == rightAnswer)
{
option3.setOnClickListener(this);
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = v.getId();
switch (position) {
case R.id.idc_questionOption1:
Intent intent = new Intent(this, GameActivity.class);
startActivity(intent);
Toast.makeText(this, "Option1", Toast.LENGTH_LONG).show();
break;
case R.id.idc_questionOption2:
intent = new Intent(this, GameActivity.class);
startActivity(intent);
Toast.makeText(this, "Option2", Toast.LENGTH_LONG).show();
break;
case R.id.idc_questionOption3:
intent = new Intent(this, GameActivity.class);
startActivity(intent);
Toast.makeText(this, "Option3", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
这里我的ArrayList包含两个Question类对象,我希望在Iterator循环启动时,然后加载我的视图进行第一次索引,然后选择我的一个选项,然后运行ArrayList的第二个索引。
但是在上面的场景中,完成循环执行了两次,然后加载了视图,并显示了最后一个索引的值。
我如何实现这一任务。请帮助我。
感谢任何帮助。提前致谢。