2010-01-26 55 views
0

我正在做一個應用程序,我必須在Timer的幫助下隨機自動顯示TextView上的數字。我能夠獲得在日誌中的隨機數不重複,但我不能打印相同的設備上,請幫我...關於android開發

問候, Akki

來源:

//RandomNumber.java 
public class RandomNumber extends Activity{ 
    static Random randGen = new Random(); 
    int tambolanum,count=0; 

    private Button previousbutton; 
    private Button startbutton; 
    private Button nextbutton; 

    int bingonum[]=new int[90]; 
    boolean fill; 

    @Override public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.numbers); 
     LinearLayout number=(LinearLayout)findViewById(R.id.numbersview); 
     final TextView randomnum=(TextView)findViewById(R.id.numberstext); 
     previousbutton=(Button)findViewById(R.id.previous); 
     nextbutton=(Button)findViewById(R.id.next); 
     startbutton=(Button)findViewById(R.id.start); 
     startbutton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // Perform action on click 
       //--- Initialize the array to the ints 0-90 
       do{ 
        fill = true; 
        //Get new random number 
        tambolanum = randGen.nextInt(90) + 1; 
        //If the number exists in the array already, don't add it again 
        for(int i = 0; i < bingonum.length; i++) 
        { 
         if(bingonum == tambolanum) 
         { 
          fill = false; 
         } 
        } 
        //If the number didn't already exist, put it in the array and move 
        //To the next position 
        if(fill == true) 
        { 
         bingonum[count] = tambolanum; 
         count++; 
        } 
       } while(count < 90); 
       for(i=0;i 
       { 
        randomnum.setText(Integer.toString(bingonum[i]); 
       } 
      } 
+2

發佈您的代碼,所以我們可以看看 – 2010-01-26 07:28:05

回答

0
+0

我用同樣的function..im能夠在TextView的獲得只有一個號碼,但不是所有的數字.. :-(......我應該怎麼做? – user259048 2010-01-26 07:24:13

0

您遇到的問題是,你在改寫這個循環的每次itteration文本:

for(i=0;i 
{ 
    randomnum.setText(Integer.toString(bingonum[i]); 
} 

你需要先建立你的字符串然後設置它。喜歡的東西:

StringBuilder sb = new StringBuilder(); 
for(i=0;i /* where's the rest of this for-statement? */ 
{ 
    sb.append(Integer.toString(bingonum[i]); 
} 
randomnum.setText(sb.toString());