jeudi 3 septembre 2015

How to fix the Handler method that include Random method

I need help in here, so i want to use Handler to auto Random my case list every 2 second

the full code is like this

public class play extends Activity implements View.OnClickListener{

int number;
Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.play);


     button1 = (Button)findViewById(R.id.button1);
     button2 = (Button)findViewById(R.id.button2);
     button3 = (Button)findViewById(R.id.button3);
     button4 = (Button)findViewById(R.id.button4);
     button5 = (Button)findViewById(R.id.button5);
     button6 = (Button)findViewById(R.id.button6);
     button7 = (Button)findViewById(R.id.button7);
     button8 = (Button)findViewById(R.id.button8);
     button9 = (Button)findViewById(R.id.button9);
     button10 = (Button)findViewById(R.id.button10);
     button11 = (Button)findViewById(R.id.button11);
     button12 = (Button)findViewById(R.id.button12);
     button13 = (Button)findViewById(R.id.button13);
     button14 = (Button)findViewById(R.id.button14);
     button15 = (Button)findViewById(R.id.button15);
     button16 = (Button)findViewById(R.id.button16);

//i had use setOnClickListener(This) to make all of button do same logic in one method
// correct me if i wrong

       button1.setOnClickListener(this);
       button2.setOnClickListener(this);
       button3.setOnClickListener(this);
       button4.setOnClickListener(this);
       button5.setOnClickListener(this);
       button6.setOnClickListener(this);
       button7.setOnClickListener(this);
       button8.setOnClickListener(this);
       button9.setOnClickListener(this);
       button10.setOnClickListener(this);
       button11.setOnClickListener(this);
       button12.setOnClickListener(this);
       button13.setOnClickListener(this);
       button14.setOnClickListener(this);
       button15.setOnClickListener(this);
       button16.setOnClickListener(this);

}



// this is the method to make all of the button do same logic with one method
// correct me if i wrong

@Override
public void onClick(View v) {

//i use the handler method to do auto-random every 2 second
// correct me if i wrong

    final Handler mHandler = new Handler();
    final int ms=2000; //2secs.
//i got error in this line
// Define cannot be resolved to a type, Syntax error on token ":", { expected after this token, Syntax error, insert ";" to complete


    Define a Runnable: 

    final Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            Random generator = new Random();
            number = generator.nextInt(16) + 1; 


             // The '16' is the number of values

             // Here, we are checking to see what the output of the random was
             switch(number) { 

              case 1:  
              button1.setVisibility(View.VISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 2: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.VISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 3:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.VISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 4:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.VISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 5:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.VISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 6:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.VISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 7:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.VISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 8:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.VISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 9:  
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.VISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 10: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.VISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 11: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.VISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 12: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.VISIBLE);                   
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 13: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.VISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 14: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.VISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 15: 
              button1.setVisibility(View.INVISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.VISIBLE);
              button16.setVisibility(View.INVISIBLE);
              break;
              case 16: 
              button1.setVisibility(View.VISIBLE);
              button2.setVisibility(View.INVISIBLE);
              button3.setVisibility(View.INVISIBLE);
              button4.setVisibility(View.INVISIBLE);
              button5.setVisibility(View.INVISIBLE);
              button6.setVisibility(View.INVISIBLE);
              button7.setVisibility(View.INVISIBLE);
              button8.setVisibility(View.INVISIBLE);
              button9.setVisibility(View.INVISIBLE);
              button10.setVisibility(View.INVISIBLE);
              button11.setVisibility(View.INVISIBLE);
              button12.setVisibility(View.INVISIBLE);                     
              button13.setVisibility(View.INVISIBLE);
              button14.setVisibility(View.INVISIBLE);
              button15.setVisibility(View.INVISIBLE);
              button16.setVisibility(View.VISIBLE);
              break;

           }


// the auto-random will be faster and faster
// correct me if i do it wrong too

    ms=ms-20; //20ms faster
            mHandler.postDelayed(this, ms);

            }
        };

    mHandler.postDelayed(mUpdateTimeTask, 100); 

}  

}

i'm sorry if you're madness look at my code, because that i was need help with people more experience to fix or give another sample code as solution for the current mistake on my code

Thanks in Advance.




Aucun commentaire:

Enregistrer un commentaire