0

I want to display the data of my database which is in txt file randomly one at a time, and it cannot repeat the data.

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


}
A--C
  • 36,351
  • 10
  • 106
  • 92
  • hows the question in my database will do not repeat again in textview. i have no idea guys.. please help me guys. – Manny Dimalanta Jan 24 '13 at 03:00
  • Try consuming the entire text file as one singular data structure (preferably an [ArrayList](http://developer.android.com/reference/java/util/ArrayList.html)). Getting random data from an array isn't too hard. I think you may want to look into [shuffling an array](http://leepoint.net/notes-java/algorithms/random/random-shuffling.html), then popping off the top of it. – yurisich Jan 24 '13 at 03:00
  • Im newbie in android almost 3 weeks. can you give me an example getting random data from array! and explained it to me.. thanks bro – Manny Dimalanta Jan 24 '13 at 05:41
  • http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates – Gina Jul 17 '13 at 07:51

0 Answers0