-1

I want to switch between 3 different strings when a button is pressed in a loop.

I have put the text in a string array but i am not sure what to use to loop the array.

    Button ranFw = (Button) findViewById(R.id.ranButFw);
    final TextView ranTx = (TextView) findViewById(R.id.ranText);



    String[] rangeTx = new String[2];
    rangeTx[0]="0 to 1700C";
    rangeTx[1]="32 to 3218F";
    rangeTx[2]="273.15 to 1973.15K";

    ranFw.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

Thanks steve

Update:

ranFw = (Button) findViewById(R.id.ranButFw); final TextView ranTx = (TextView) findViewById(R.id.ranText);

     final String[] rangeTx = new String[3];//String[2] means index from 0 to 1
    rangeTx[0] = "0 to 1700C";
    rangeTx[1] = "32 to 3218F";
    rangeTx[2] = "273.15 to 1973.15K";
    ranFw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (index) {
                case 0:
                    // if we are using index 0, set the text to index 1 text and change index to 1
                    index = 1;
                    ranTx.setText(rangeTx[index]);
                    break;
                case 1:
                    index = 2;
                    ranTx.setText(rangeTx[index]);
                    break;
                case 2:
                    index = 0;
                    ranTx.setText(rangeTx[index]);
                    break;

            }

        }

    });

No errors but seems to be working properly

KrispyKreme
  • 39
  • 1
  • 8

3 Answers3

0

A simple switch/case block will handle this nicely.

// First, keep track of which index you are using
int index = 2; // initialize it to 2 so that the first time u click the button you get the first text

// loop through
switch (index) {
    case 0:
        // if we are using index 0, set the text to index 1 text and change index to 1
        index = 1;
        ranTx.setText(rangeTx[index]);
    break;
    case 1:
        index = 2;
        ranTx.setText(rangeTx[index]);
    break;
    case 2:
        index = 0;
        ranTx.setText(rangeTx[index]);
    break;
}

The switch/case code should go inside your onclick listener, while the index should be outside, possibly global. Then, everytime the button is clicked the text will change to the next one.

Also, you initialized your string array wrong; it should be [3], not [2].

Aify
  • 3,543
  • 3
  • 24
  • 43
0

First your array contains 3 strings, so it's new String[3].

To iterate the array you can use a counter. You reset it everytime the last value is reached:

final String[] rangeTx = new String[3];
rangeTx[0]="0 to 1700C";
rangeTx[1]="32 to 3218F";
rangeTx[2]="273.15 to 1973.15K";

ranFw.setOnClickListener( new View.OnClickListener() {
    private int mCounter;
    @Override
    public void onClick(View v) {
        Log.e("TAG", rangeTx[mCounter]);
        if (++mCounter == 3) mCounter = 0;
    }
});
Simas
  • 43,548
  • 10
  • 88
  • 116
0

In this Case, you Have more than one choice for example you have For and While loop, Switch Case and if else statements . But the most feasible solution is random function , that will generate a random number between 0 ,1 and 2. you just set that value to button. Hence Text will be change.

final String[] rangeTx = new String[3];//String[2] means index from 0 to 1  
        rangeTx[0] = "0 to 1700C";
        rangeTx[1] = "32 to 3218F";
        rangeTx[2] = "273.15 to 1973.15K";
        ranFw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Random rand = new Random();//initializing function

                int randomNum = rand.nextInt((2 - 0) + 1) + 0;
                //max=2; min =0; because array has 3 length 0, 1, 2
               //rand.nextInt((max - min) + 1) + min; 

                 if(randomNum<3)
                    ranFw.setText(rangeTx[randomNum]);
                 else 
                    ranFw.setText(rangeTx [0]);

            }

        });

you can also this random number with switch case too. Hope this will help. happy coding .

Fasiha
  • 488
  • 3
  • 11