0

edit. I'm trying to make a game where there is 4 buttons each button has a set value. buttone is 1 buttontwo is 2 and so on. and on each click of the button it takes the value from what ever button is pressed and add it to an array, the number of buttons that you have to press is set by the intent from the main activity and the buttons that have to be press is set by the randomNums array. eg. if the randomNums give you 1,2,1,1,2.3,3,4. green button must be pressed 3 times blue button must be pressed 2 times yellow button must be pressed once. buttonOne must be pressed 3 times. buttonTwo must be pressed 2 times. buttonThree must be pressed 2 times. buttonFour must be pressed once times. I have the randomNums which gives you the up to 12 number from 1-4. I have the buttons set up so that if the randomNums gives you 1,1,1,2,3,3. ButtonOne will be green buttonTwo will be yellow and buttonThree will be blue for 3 seconds then all the buttons turn grey. but where I'm having trouble is how do u set a button to have a set value. eg. if I press buttonOne 3 times it will enter into an array like so 1,1,1. so the question is how do set a fixed value to a button so that every time it is press it send a fix value to an array.

sry if I was not clear the 1st times around if u have any more question or if you need me to explain it again or want to see the code that I have to set the color of the button play let me know . thanks

hi I'm very new to android studio, I have 4 buttons and I want each to have a fixed value.

ButtonOne =1;
ButtonTwo =2;
ButtonThree =3;
ButtonFour =4;

and when a button is pressed I want to add that fixed button value to an array. what I have is sending an ++ number back and not sending a fixed value.

so my question is how do you send a multiple fixed int from four buttons OnClickListener to an array?

public int counterOne = 0;

final Button buttons[] = {
            (Button) findViewById(R.id.ButtonOne),
            (Button) findViewById(R.id.ButtonTwo),
            (Button) findViewById(R.id.ButtonThree),
            (Button) findViewById(R.id.ButtonFour)
    };

    buttons[0].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counterOne = counterOne+1;
            CheckSET.setText(""+counterOne);
        }
    });


   final int pressed[] =new int [12];
    end.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String stringlevel = ""+CheckSET.getText();
            String b = "";
            final int intLevel = Integer.parseInt(stringlevel);
            for (int i = 0; i < 12;i++){
                //b = ""+ b +""+ pressed[i];
                //pressed[i] = pressed[i]+1;
                //if(intLevel==1){
                //    pressed[i] = pressed[i]+1;
                //}else if (intLevel ==2){
                //   pressed[i] = pressed[i]+2;
                //}
                pressed[0] = counterOne;
                b = ""+ b +""+ pressed[i];
                diff.setText(b);
            }
        }
    });

the end button is to add all the button clicks to the array. but if there is a better way then doing an end button to run the button array that would be great. could you do a for() and have all the buttons inside the for and each time they are pressed it adds it to the array?

I have a random array set up all ready that picks 12 numbers from 1-4 and does not let any number repeat more then 3 times.

 private int[] pickNums() {
    Random rand = new Random();
    int randomNums[] = new int[12];
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    for (int i = 0; i < 12; i++) {
       randomNums[i] = rand.nextInt(4)+1;
        if (randomNums[i] == 1) {
            one = one + 1;
            if (one>3){
                i=i-1;
            }
        } else if (randomNums[i] == 2) {
            two = two + 1;
            if (two>3){
                i=i-1;
            }
        } else if (randomNums[i] == 3) {
            three = three + 1;
            if (three>3){
                i=i-1;
            }
        } else if (randomNums[i] == 4) {
            four = four + 1;
            if (four>3){
                i=i-1;
            }
        }
    }
return randomNums;
}

my plan is to do a bubble sort on the randomNums and on the buttonPress array to see if the buttons that are pressed all ==. but I'm having a lot of trouble with the buttonpress array

3
  • It's not really clear what do you want to do and what is your problem. To begin, you might want to find every button separately like: Button btt1 = (Button) findViewById(R.id.ButtonOne), Button btt2 = (Button) findViewById(R.id.ButtonTwo), etc. And then set a click listener for each btt individually. And make for example. btt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {counter+=3}) Commented Mar 9, 2017 at 20:46
  • Clarification question: do I understand correctly that you're trying to take 12 numbers from the user that can be 1-4 with the buttons, and then do something with the collected numbers when the end button is clicked? Commented Mar 9, 2017 at 20:53
  • I add more info to try clear an question you have sry if I was clear the 1st times around Commented Mar 9, 2017 at 21:34

1 Answer 1

1

I am not sure what you asking but may be this will help you (Instead of Array you should use list)

      private List<Integer> pressedNums = new ArrayList<>();

Button button1,button2,button3,button4;
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        pressedNums.add(1)

    }
});

similarly for button2,3,4 add 2,3,4

at last you can use loop

    int total = 0;
     for(Integer i : pressedNums){
       total += i;
    }
   textField.setText(Integer.toString(total));
Sign up to request clarification or add additional context in comments.

3 Comments

thanks ill give it a shot. just a quick question can you do a bubble sort on arrayList and are you able to compare an arraylist to an array?
ok so I tried it out and got it works but it does not do what I would like it to do. I want the button press only to return one fixed num. eg, you press the button 5 times and it will add to the array/ list array {1,1,1,1,1} at the moment it adding each number and returning 5
never mind I got it working I change it a little and did textfield.setText(""+pressedNums). thank you very much for you help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.