0

i want to get the next item on an array on button click.

array is like this :

String[] pickupLinesItems = { "a", "b", "c" };

then i have this for the spinner

public void onItemSelected(org.holoeverywhere.widget.AdapterView<?> parent,
        View view, int position, long id) {

    position = spinner.getSelectedItemPosition();

    SpinnerAdapter adap = spinner.getAdapter();

    if (adap.equals(pickupLinesAdapter)) {
        switch (position) {
        case 0:
            stopPlaying();
            speakMedia = MediaPlayer.create(this, R.raw.a);
            break;
        case 1:
             stopPlaying();
             speakMedia = MediaPlayer.create(this, R.raw.b);
             break;
        case 2:
             stopPlaying();
             speakMedia = MediaPlayer.create(this, R.raw.c);
             break;

        }
    }
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    int index = 0;

    switch (v.getId()) {
    case R.id.next:
        // here i want to go on the next item on array
        break; 
    case R.id.back:
                    // here i wanna go back one item on the array
        break;
    }
}

how would i do this, i've tried everything and cant seem to get it. i tried to do an if statement but nothing.

2
  • So, like, if the spinner is on b, you want a button press to change it to c? Commented Feb 14, 2014 at 19:51
  • 1
    use one int value,just increment and decrements that Commented Feb 14, 2014 at 19:53

2 Answers 2

3

Need a variable to hold the current index of pickupLinesItems declared at the same scope as String[] pickupLinesItems = { "a", "b", "c" };

For example:

int pickupLinesItemIndex = 0;

Then you could do something like this:

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

    switch (v.getId()) {
    case R.id.next:
        if(pickupLinesItemIndex < pickupLinesItems.length)
        {
          String pickupLine = pickupLinesItemIndex[++pickupLinesItemIndex];
        }
        break; 
    case R.id.back:
        if(pickupLinesItemIndex > 0)
        {
          String pickupLine = pickupLinesItemIndex[--pickupLinesItemIndex];
        }
        break;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

i'm sure you mean ++pickupLinesItemIndex and --pickupLinesItemIndex and you must check pickupLinesItemIndex that not became bigger than size of array or lower than zero
you need pickupLinesItems.length - 1 for checking max.
tried this ang nothing happened, doesnt change, doesnt give me an error, nothing.
0

Assuming my comment is correct, you could do something like:

//in your onClick
case R.id.next:
    int current = spinner.getSelectedItemPosition();
    spinner.setSelection(current++);
    break;

You could do the same thing in back. Also, make sure to check for boundary conditions (if you're on c, make sure you go to a and not attempt to reach out of the array's bounds).

2 Comments

I will say current++%3, or he should check for IndexOutOfBoundException
i tried this before and it didnt work then and still doesnt and to be honest i dont know why. it doesnt give me an arror or anything it just doesnt move on to the next.

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.