数据库文本文件随机而不重复数据?

时间:2013-01-24 02:57:16

标签: android database random

我想一次一个地随机显示我的数据库中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();
            } 


}

0 个答案:

没有答案