我试图建立这个测验计划,其中一个人被问到一个问题,然后必须回答,这是一个双人游戏,他们只是通过所有问题,游戏结束.
到目前为止我设法做的是将数组(strings.xml文件中的数组)加载到arraylist然后从0到arraylist.size的随机id然后显示该字符串(问题)的通用代码然后它删除随机ID并采用一个新的.
但是当我运行arraylist中的所有id时程序崩溃,我的代码看起来像这样:http://pastebin.com/MaEj49BL
重要的是这个:
public void gameCode()
{
setContentView(R.layout.game);
if (mList == null)
{
String[] mQuestions = getResources().getStringArray(R.array.mQuestions);
mList = new ArrayList();
Collections.addAll(mList, mQuestions);
}
else if (mList.isEmpty())
{
m = (TextView)findViewById(R.id.textView1);
m.setText("You've run out of questions!");
}
if (wList == null)
{
String[] wQuestions = getResources().getStringArray(R.array.wQuestions);
wList = new ArrayList();
Collections.addAll(wList, wQuestions);
}
else if (wList.isEmpty())
{
w = (TextView)findViewById(R.id.textView1);
w.setText("You've run out of questions!");
}
//Takes a random ID from the arraylist
int rndm = generator.nextInt(mList.size());
int rndw = generator.nextInt(wList.size());
//Sets the Strings to a random number from one of the array lists
String qMan = (String) mList.get(rndm);
String qWoman = (String) wList.get(rndw);
if(i == 0)
{
//Defines that w is textView1 with the value of qWoman
w = (TextView)findViewById(R.id.textView1);
w.setText(qWoman);
wList.remove(rndw);
i = 1;
final Button buttonNext = (Button) findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
else
{
m = (TextView)findViewById(R.id.textView1);
m.setText(qMan);
mList.remove(rndm);
i = 0;
final Button buttonNext = (Button) findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
gameCode();
}
});
}
}
}
来自pastebin的源代码中有注释.现在我只需要将文本更改为“你已经用完了问题”