如何将数据库中的数据列表设置为在基于android的应用程序中随机显示?

如何将数据库中的数据列表设置为在基于android的应用程序中随机显示?,android,database,random,arraylist,runtime-error,Android,Database,Random,Arraylist,Runtime Error,我试图在我的数据库中显示来自数据库示例的随机数据,我总共有20个问题。我想在每次访问应用程序时随机显示10个QN。这是我第二次访问它,它将显示以前没有显示过的其他10个QN 我的代码: ArrayList<Integer> randList = new ArrayList<Integer>(); Random randGenerator = new Random(); ArrayList<String> list2 = new ArrayLi

我试图在我的数据库中显示来自数据库示例的随机数据,我总共有20个问题。我想在每次访问应用程序时随机显示10个QN。这是我第二次访问它,它将显示以前没有显示过的其他10个QN

我的代码:

ArrayList<Integer> randList = new ArrayList<Integer>();
    Random randGenerator = new Random();

    ArrayList<String> list2 = new ArrayList<String>(db.selectData("MCQ",new String [] {"_id"}, "testId=0", null, null, null, null));

    //random 10 n.o
    for(int i=0;i<10;i++)
    {
        int randNum = randGenerator.nextInt(list2.size());

        while (randList.contains(randNum))
        {
            randNum = randGenerator.nextInt(list2.size());
        }
        randList.add(randNum);
        Log.d("rand no:"+i,String.valueOf(randNum));
    }

    ArrayList<String> idList = new ArrayList<String>();
    //get the 10 _id that are chosen
    for (int i=0; i<10;i++)
    {
        idList.add(list2.get(Integer.valueOf(randList.get(i))));
        Log.d("id:"+i,String.valueOf((list2.get(Integer.valueOf(randList.get(i))))));
    }

    ArrayList<String> list3 = new ArrayList<String>(db.selectData("MCQ",new String [] {"Question"}, null, null, null, null, null));
    Log.d("size",String.valueOf(list3.size()));
    ArrayList<String> qnsList = new ArrayList<String>();

    // == set question in arrangement of randList == //
    for(int i=0;i<=list3.size();i++)
    {
        qnsList.add(list3.get(i));
        Log.d("_id:"+i,String.valueOf(list3.get(i)));
    }
还有更多,但我认为这是没有用的,因为我试过只使用_id=19运行,它可以工作,但一旦我更改为20,它就会崩溃。

更改此行:

for(int i=0;i<=list3.size();i++)
将是:

for(int i=0;i<list3.size();i++)

在上一次迭代中,当i=list3.size时,您在循环1中迭代了太多次,在执行list3.geti时,您会得到一个IndexOutOfBoundsException,因为列表索引是基于0的,所以第二十个也是最后一个元素实际上位于索引19处。

请记住,SQLite的_id不是基于0的

for(int i=0;i<list3.size();i++)