0

我想一次随机显示一个在txt文件中的数据库数据,并且不能重复数据。

private void question() {
    // TODO Auto-generated method stub


          InputStreamReader inputStream = new InputStreamReader
            (getResources().openRawResource(R.raw.question1));
            BufferedReader br = new BufferedReader(inputStream);

            //number of lines in the database numLines
            int numLines= 10;
            Random r = new Random();
            int desiredLine = r.nextInt(numLines);

            String theLine="";
            int lineCtr = 0;


            try {


                while ((theLine = br.readLine()) != null)   {

                if (lineCtr == desiredLine) {

                    StringTokenizer st = new StringTokenizer(theLine,",");
                    while(st.hasMoreTokens())
                     {   
                         //reading and getting data from the database","
                         String a = st.nextToken();
                         String b = st.nextToken();
                         String c = st.nextToken();
                         String d = st.nextToken();
                         String e = st.nextToken();
                         String f = st.nextToken();

                         //button setText
                         question.setText(a);
                         ca.setText(f);
                         firstbutton.setText(e);
                         secondbutton.setText(d);
                         thirdbutton.setText(b);
                         fourthbutton.setText(c);
                     //assigning
                         firstholder =e;
                         secondholder =d;
                         thirdholder=b;
                         fourthholder =c;

                         break;
                     }

                  }
                  lineCtr++;

                 }


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 


}
4

0 回答 0