Reputation: 87
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)
Upvotes: 4
Views: 2206
Reputation:
Your <item>--Pilih--</item>
is causing a problem, as it is taking up the position of the first item of your spinner. So by the time 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 item at the top of your spinner: How to make an Android Spinner with initial text "Select One"
You can change the visibility to gone, that is a good idea, however you need to implement separate onItemSelectedListeners for each spinner.
resiko,untung1,untung2,untung3
as they are separate elements and have varying sized arrays.
Although it seems like more work, it's important to keep UI elements interactions separate.
Now if there is a method that is common to a user selection, this can be modularised.
Example: So if several different item selections cause the background color to turn yellow in a method called:
turnBackgroundYellow()
then this method can be placed in as many on item selected events as you please.
However the reverse does not work.
Each unique spinner needs to have it's listeners attached to it specifically for that spinner.
Make these class variables, so you can pass them as parameters into a 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
Upvotes: 3