There are 4 Spinnners:
resiko,untung1,untung2,untung3
The user picks resiko first to proceed and the app will show which Spinner
will be visible (untung1/untung2/untung3)
Trying to make dynamic data Spinner, when you click an item inside the first Spinner, the others will be gone, and the desired one will be visible, working so far.
The problem is is when i pick "tinggi" from the Spinner resiko, it get an error: java.lang.IndexOutOfBoundsException
I also tried reading other posts, but still not sure what should I do.
I tried using getSelectedItem()
before, but when I want to take the desired data from the visible Spinner, which is selected by the user, the app didn't pick the selected item itself, instead it picked the first data in the visible Spinner.
(let's say there's 2 value in the Spinner, A and B; the user pick B, but the program picks A)
in example:
the user picks "rendah" in the "resiko" Spinner, and then the next visible Spinner is untung2, then the user picks "sedang" in that Spinner,
but the program picks "--pilih--" instead of "sedang"
That's why I switched to getItemAtPosition(position).toString();
strings.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 out bound when i pick "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
}
});
logcat :
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)