Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

java - android, implementing setOnItemSelectedListener to make dynamic spinner

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For starters:

public void SelectedItemMethod(String item){
    if (item.equals("Sangat Rendah")) {
            untung1.setVisibility(View.VISIBLE);
            untung2.setVisibility(View.GONE);
            untung3.setVisibility(View.GONE);
            // And remove this! This will throw an error.
            // untungx = untung1.getSelectedItem().toString();
    } else if (item.equals("Rendah")){
    .../...

You are passing the value of the selected item as a parameter into your SelectedItemMethod as String item.

So when you call:

SelectedItemMethod(untung1_);

String item = untung1_

Also:

The parent does not exist:

@Override
public void onNothingSelected(AdapterView<?> mRelative) {
    //DO WHATEVER OR NOTHING
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...