Android/Java:检查 arraylist 是否为空,然后启动另一个活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7966991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:15:13  来源:igfitidea点击:

Android/Java: Check if arraylist is empty and then launch another activity

javaandroidarraysarraylist

提问by psalomonsen

Im trying to build this Quiz program where a person is asked a question from and then has to answer, it's a game for two persons, and they just go through all the questions and the game ends.

我试图建立这个测验程序,一个人被问到一个问题,然后必须回答,这是一个两个人的游戏,他们只是通过所有的问题,游戏就结束了。

What i've managed to make so far is the general code for loading the array (the array in the strings.xml file) into an arraylist and then take a random id from 0 to arraylist.size and then show that string (Questions) then it deletes the random id and takes a new one.

到目前为止,我设法制作的是将数组(strings.xml 文件中的数组)加载到数组列表中的通用代码,然后从 0 到 arraylist.size 取一个随机 id,然后显示该字符串(问题)然后它删除随机 id 并获取一个新的。

But when ive run through all the id's in the arraylist the program crashes, my code looks like this: http://pastebin.com/MaEj49BL

但是当我遍历 arraylist 中的所有 id 时,程序崩溃了,我的代码如下所示:http: //pastebin.com/MaEj49BL

The important part is this:

重要的部分是这样的:

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();
                }
            });
       }

}  

}

}

There is comments in the source code from the pastebin. Right now I just need to to change the text to "You've run out of questions"

pastebin 的源代码中有注释。现在我只需要将文本更改为“你的问题已经用完了”

回答by Chris

You're checking whether or not the 2 arrays are empty but you're not short-circuiting the remainder of the logic when they are...

您正在检查 2 个数组是否为空,但是当它们为空时,您并没有短路其余的逻辑...

if mList and wList are empty, you should bypass the remainder of the method or, at a minimum, check that mList.size() > 0and wList.size()>0before trying to get the next random question from each array.

如果 mList 和 wList 为空,您应该绕过该方法的其余部分,或者至少mList.size() > 0and wList.size()>0在尝试从每个数组中获取下一个随机问题之前进行检查。

回答by anirudh bhatnagar

Use CollectionUtils.isEmpty() which is null safe

使用 CollectionUtils.isEmpty() 是空安全的