There are 4 Spinnners:
resiko,untung1,untung2,untung3
The user picks resiko spinner first to proceed and the app will show which Spinner will be visible (untung1/untung2/untung3)
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>
declaration :
String resikox_;
String untung1_;
String untung2_;
String untung3_;
static Spinner resiko;
static Spinner untung1;
static Spinner untung2;
static Spinner untung3;
the dynamic spinner code
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.spinner_resiko_string,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
resiko.setAdapter(adapter);
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
resikox_ = mRelative.getItemAtPosition(position).toString();
SelectedItemMethod(resikox_);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//DO WHATEVER OR NOTHING
}
});
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(getActivity(), R.array.spinner_return_string,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
resiko.setAdapter(adapter2);
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
untung1_ = mRelative.getItemAtPosition(position).toString();
SelectedItemMethod(untung1_);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//DO WHATEVER OR NOTHING
}
});
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(getActivity(), R.array.spinner_return_string2,android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
resiko.setAdapter(adapter3);
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
untung2_ = mRelative.getItemAtPosition(position).toString();
SelectedItemMethod(untung2_);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//DO WHATEVER OR NOTHING
}
});
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(getActivity(), R.array.spinner_return_string3,android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
resiko.setAdapter(adapter4);
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
untung3_ = mRelative.getItemAtPosition(position).toString();
SelectedItemMethod(untung3_);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//DO WHATEVER OR NOTHING
}
});
SelectedItemMethod :
// Class method to do your item selection stuff.
public void SelectedItemMethod(String item){
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);
}
}
i tried using getItemAtPosition(position) instead of getSelectedItem() before. but the "position" cant be resolve
then i got java.lang.NullPointerException and app crash
logcat :
12-23 21:13:13.171 14033-14033/com.example.fabio.tabdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fabio.tabdrawer, PID: 14033
java.lang.NullPointerException
at com.example.fabio.tabdrawer.Menu_PIAF.SelectedItemMethod(Menu_PIAF.java:1336)
at com.example.fabio.tabdrawer.Menu_PIAF$4.onItemSelected(Menu_PIAF.java:234)
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)
where i did it wrong now ?
line 1336 : its the one in SelectedItemMethod
if (resikox_.equals("Sangat Rendah"))
line 234 : is this
SelectedItemMethod(untung3_);
See Question&Answers more detail:
os