开发者

Regarding android Development

开发者 https://www.devze.com 2022-12-17 08:33 出处:网络
I am doing an application in which I have to display the numbers on TextView randomly and automatically with the help of Timer. I am able to get the random Numbers in the log without repeating, but I

I am doing an application in which I have to display the numbers on TextView randomly and automatically with the help of Timer. I am able to get the random Numbers in the log without repeating, but I am not able to print the same on device please help me...

Regards, Akki

Source:

//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)
                    {
                        bingo开发者_如何学运维num[count] = tambolanum;
                        count++;
                    }
                } while(count < 90);
                for(i=0;i
                {
                    randomnum.setText(Integer.toString(bingonum[i]);
                } 
            }


setText(CharSequence text)


The problem you're having is that you're overwriting your text in every itteration of this loop:

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

You need to build your string first then set it. Something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号