Update spinner items based on the selected item of

2019-04-06 11:12发布

I am trying to update a spinner by a selected item from another spinner. The following code block describes how I did it.

I could successfully update the spinner3 using the if statement inside onItemSelected() method. But my problem is that it seems that a loop just keeps running which automatically selects value "5" from spinner2 (so the Toast just keeps spitting out "You've chosen 5").

When the activity is loaded, the Toast will display each selection from all three spinners. Does it mean that the first selection for each spinner is done when an adapter is set to a spinner?

And does binding adapter3 to spinner3 (in the if statement) trigger a new selection of spinner2?

Thanks in advance!

public class WheelchairHelperMain extends Activity{

Spinner spinner2;
Spinner spinner3;
ArrayAdapter<CharSequence> adapter3 ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wheelchair_helper_main);

    Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.destinations_list, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());

    spinner2 = (Spinner)findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,R.array.departure_timeH_list,android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);
    spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());

    spinner3 = (Spinner)findViewById(R.id.spinner3);
    spinner3.setOnItemSelectedListener(new MyOnItemSelectedListener());



}

public class MyOnItemSelectedListener implements OnItemSelectedListener{


    public void onItemSelected(AdapterView<?> parent, View v, int pos,long id) {
        // TODO Auto-generated method stub
        //use the selected station and departure time to calculate the required time
            Toast toast = Toast.makeText(parent.getContext(),"You've chosen: " + parent.getItemAtPosition(pos), 2);
            toast.show();

            if (spinner2.getSelectedItem().equals("5")){
                adapter3 = ArrayAdapter.createFromResource(WheelchairHelperMain.this,R.array.departure_timeH5M_list, android.R.layout.simple_spinner_item);
                adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner3.setAdapter(adapter3);

            Log.v("spinner2","5");
            }else if(spinner2.getSelectedItem().equals("6")){

                adapter3 = ArrayAdapter.createFromResource(WheelchairHelperMain.this,R.array.departure_timeH6M_list, android.R.layout.simple_spinner_item);
                adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner3.setAdapter(adapter3);

                Log.v("spinner2","6");
            }

    }



    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}
}

The following are the xml files. This is the string-array for spinner2:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="departure_timeH_prompt">何時?</string>
<string-array name="departure_timeH_list">
    <item>5</item>
    <item>6</item>
    <item>  7   </item>
    <item>  8   </item>
    <item>  9   </item>
    <item>  10  </item>
    <item>  11  </item>
    <item>  12  </item>
    <item>  13  </item>
    <item>  14  </item>
    <item>  15  </item>
    <item>  16  </item>
    <item>  17  </item>
    <item>  18  </item>
    <item>  19  </item>
    <item>  20  </item>
    <item>  21  </item>
    <item>  22  </item>
    <item>  23  </item>
    <item>  24  </item>
    <item>  0   </item>       
    </string-array>
</resources>

The string-array for spinner3:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="departure_timeM_prompt">何分?</string>
 <string-array name="departure_timeH5M_list">
    <item>  5   </item>
    <item>  24  </item>
    <item>  38  </item>
    <item>  52  </item>
</string-array>
<string-array name="departure_timeH6M_list">
    <item>  01  </item>
    <item>  08  </item>
    <item>  17  </item>
    <item>  25  </item>
    <item>  34  </item>
    <item>  41  </item>
    <item>  47  </item>
    <item>  57  </item>
 </string-array>
</resources>

I just found my problems.

In the original code:

  spinner3.setAdapter(adapter3);

is called inside the onItemSelectedListener. When an adapter is set to an AdapterView, the first item on the adapter will be automatically chosen once. So, it will trigger the listener again.

And since I didn't set a flag to distinguish whether the selection comes from spinner2 and put the spinner3.setAdapter(adapter3), it will just keep running the "choosing first item on adapter3 -> check if the selected item is 5" loop.

So, inserting the following flag in the original code works for me:

if (parent.getId()==2131165201){
  //code to check selected item and define new adapter3 for spinner3

3条回答
SAY GOODBYE
2楼-- · 2019-04-06 11:29

If i understood your question correctly spinner is selecting a value by default.Yes that is correct if spinner sets to an adapter it will selects to first value. you have to write simple hack in onItemselectedListener to clear that default selection. take one int variable=-1 in my code it is ageposition im taking one boolean value age selected to find the default selection. if(ageselected) means spinner selected by user.

finally use ageSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

 class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
            if(ageposition==-1)
            {
                ageselected=false;
                ageposition=pos;
            }
            else
            {
                ageposition=pos;
                ageselected=true;
                childage=age[pos];
            }


        }
        public void onNothingSelected(AdapterView<?> parent) {
          // Dummy
        }
    }

Hope the above code helps you

查看更多
成全新的幸福
3楼-- · 2019-04-06 11:31

i would suggest u use the position returned by the onItemclickListener and use it in an array initializing array adapter for that spinner: like---

`

String hrs[]=getStringArray(R.array.departure_timeH6M_list);
  if(hrs[pos].equals("5"))
    { 

    }

`

and so on...

查看更多
不美不萌又怎样
4楼-- · 2019-04-06 11:42

When the Spinner change put the data Inside the adapter of the next spinner . And the just write adapter.notifyDataSetChanged(); The content Inside the next spinner will change.

查看更多
登录 后发表回答