Java – Indexes in Spinner are out of bounds

Indexes in Spinner are out of bounds… here is a solution to the problem.

Indexes in Spinner are out of bounds

There are 4 Spinnners:
resiko,untung1,untung2,untung3

The user first selects resiko to continue, and the app will show which Spinner is visible (untung1/untung2/untung3).

Try to make dynamic data Spinner, when you click on one of the items in the first Spinner, the others will disappear, while the desired ones will be visible so far.
The problem is that when I select “tinggi” from Spinner resiko, I get the error: java.lang.IndexOutOfBoundsException

I’ve also tried reading other posts but still not sure what I should do.
I tried using getSelectedItem() earlier, but when I wanted to get the required data from the visible Spinner selected by the user, the application did not select the selected item itself, but the first data in the visible Pinner.
(Suppose there are 2 values in Spinner, A and B; The user selects B, but the program selects A).

For example:
The user selects “rendah” in “resiko” Spinner, then the next visible Spinner is untung2, and then the user selects “sedang” in that Spinner.
But the program chose “–pilih–” instead of “sedang”
That’s why I switched to getItemAtPosition(position).toString();

String .xml

<string-array name="spinner_resiko_string">
    <item>--Pilih--</item>
    <item>Sangat Rendah</item>
    <item>Rendah</item>
    <item>Sedang</item>
    <item>Tinggi</item>
</string-array>

<string-array name="spinner_return_string">
    <item>--Pilih--</item>
    <item>Rendah</item>
</string-array>

<string-array name="spinner_return_string2">
    <item>--Pilih--</item>
    <item>Rendah</item>
    <item>Sedang</item>
</string-array>

<string-array name="spinner_return_string3">
    <item>--Pilih--</item>
    <item>Rendah</item>
    <item>Sedang</item>
    <item>Tinggi</item>
</string-array>

Spinner Declaration:

    final Spinner resiko = (Spinner) mScrollView.findViewById(R.id.spinner_resiko);
    final Spinner untung1 = (Spinner) mScrollView.findViewById(R.id.spinner_return1);
    final Spinner untung2 = (Spinner) mScrollView.findViewById(R.id.spinner_return2);
    final Spinner untung3 = (Spinner) mScrollView.findViewById(R.id.spinner_return3);

Spinner in XML:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_return1"
    android:entries="@array/spinner_return_string"
    android:layout_marginLeft="10dp"/>
<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_return2"
    android:entries="@array/spinner_return_string2"
    android:layout_marginLeft="10dp"/>
<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_return3"
    android:entries="@array/spinner_return_string3"
    android:layout_marginLeft="10dp"/>

Code version 1 (error index when I select “tinggi” in resiko spinner) [using getItematPosition]:

 resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
            String resikox = mRelative.getItemAtPosition(position).toString();
            if (resikox.equals("Sangat Rendah")) {
                untung1.setVisibility(View.VISIBLE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
                untungx = untung1.getItemAtPosition(position).toString();
            }
            else if (resikox.equals("Rendah")){
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.VISIBLE);
                untung3.setVisibility(View.GONE);
                untungx = untung2.getItemAtPosition(position).toString();
            }
            else if (resikox.equals("Sedang") || (resikox.equals("Tinggi"))) {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.VISIBLE);
                untungx = untung3.getItemAtPosition(position).toString();
            } else {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
             Another interface callback
        }
    });

Version 2 (using getItemSelected).

 //set spinner
    resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
            String resikox = mRelative.getItemAtPosition(position).toString();
            if (resikox.equals("Sangat Rendah")) {
                untung1.setVisibility(View.VISIBLE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
                untungx = untung1.getSelectedItem().toString();
            }
            else if (resikox.equals("Rendah")){
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.VISIBLE);
                untung3.setVisibility(View.GONE);
                untungx = untung2.getSelectedItem().toString();
            }
            else if (resikox.equals("Sedang") || (resikox.equals("Tinggi"))) {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.VISIBLE);
                untungx = untung3.getSelectedItem().toString();
            } else {
                untung1.setVisibility(View.GONE);
                untung2.setVisibility(View.GONE);
                untung3.setVisibility(View.GONE);
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
             Another interface callback
        }
    });

Log:

12-23 19:37:23.221  30433-30433/com.example.fabio.tabdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fabio.tabdrawer, PID: 30433
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.Arrays$ArrayList.get(Arrays.java:66)
        at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
        at android.widget.AdapterView.getItemAtPosition(AdapterView.java:831)
        at com.example.fabio.tabdrawer.Menu_PIAF$1.onItemSelected(Menu_PIAF.java:183)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:964)
        at android.widget.AdapterView.access$200(AdapterView.java:49)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:928)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5487)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

Solution

Your <item>-

-Pilih--</item> is causing problems because it occupies the position of the first item in the spinner item. So when you select the last item, Tinggi it throws an index out-of-bounds error.

This will give you advice on how to create a non-selectable project on top of the spinner :How to make an Android Spinner with initial text “Select One”

It’s a good idea that you can change the visibility to disappear, but you’ll need to implement separate onItemSelectedListeners for each spinner.
Resiko, Untung1, Untung2, Untung3 because they are independent elements and have arrays of different sizes.

While it may seem like more work, it’s important to keep UI elements interactive separate.

Now, if there is a user who chooses a common method, it can be modularized.

Example:
So if several different item selections cause the background color to turn yellow in one method:

turnBackgroundYellow()

This method can then be placed in any number of project selection events.

But not the other way around.

Each unique spinner requires its listener to be attached specifically to that spinner .

Create these class variables to pass them as parameters to the class method.

String resikox_;
String untung1_;  and the others

 Create an ArrayAdapter using the string array and a default spinner layout
 Create a separate one for each spinner resiko,untung1,untung2,untung3
ArrayAdapter<CharSequence> adapter = ArrayAdapter
            .createFromResource(getActivity(), R.array.dataobjects_array,
                    android. R.layout.simple_spinner_item);
 Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(
            android. R.layout.simple_spinner_dropdown_item);
 Apply the adapter to the spinner
resiko.setAdapter(adapter);
 Create a separate one for each spinner resiko,untung1,untung2,untung3
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

resikox_ = parent.getItemAtPosition(position).toString();
        SelectedItemMethod(resikox_)
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        DO WHATEVER OR NOTHING
                }
});

 Class method to do your item selection stuff.
public void SelectedItemMethod(String item){

if (item.equals("Sangat Rendah")) {
        untung1.setVisibility(View.VISIBLE);
        untung2.setVisibility(View.GONE);
        untung3.setVisibility(View.GONE);
    }
    etc, etc for all your spinners or break it up, as you please

Related Problems and Solutions