简体   繁体   中英

Updating spinner after selecting another spinner

Hey guys i just started android app making a couple days ago, im trying to update one spinner information with another spinner. Before you start hating ive already checked the other answers and tried them all but for some reason none work! its supposed to get an array from an xml string file and fill the spinner with that i dont know if ive made a mistake in my code or something but it just doesnt seem to work. Heres the code part that doesnt work:

public class NextBusesActivity extends Activity {

private Spinner spinnerRoutes;
private Spinner spinnerStops;

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

    spinnerStops = (Spinner)findViewById(R.id.nextBusStopsSpinner);
    spinnerRoutes = (Spinner)findViewById(R.id.nextBusRoutesSpinner);


    spinnerRoutes.setOnItemSelectedListener(spinnerBusRouteHandler);
}


private OnItemSelectedListener spinnerBusRouteHandler= new OnItemSelectedListener(){

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        Log.d(MainActivity.DEBUGTAG, "Made it into the listener");

        //if (parent.getItemAtPosition(pos).toString().contains("Trent")){

            Log.d(MainActivity.DEBUGTAG, "Made it to Trent west bank     choice");
            ArrayAdapter<CharSequence> adapter =     ArrayAdapter.createFromResource(getApplicationContext(), 
                    R.array.westbank_stops,     android.R.layout.simple_spinner_item);
                     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spinnerStops.setAdapter(adapter);

            Log.d(MainActivity.DEBUGTAG, "Spinner refill worked     westbank");
        //}

    }

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

    }

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_next_buses, menu);
    return true;
}
}

Thanks for the help guys.

Your problem is not in the above code. You are probably trying to set the selection of the first spinner manually in the onCreate and expect that the ItemSelectListener would do it's job. Well it does not. The Listener only becomes active after the onCreate code is executed entirely.

So if you do this:

@Override
public void onCreate(Bundle savedInstanceState) {
...
spinnerRoutes.setOnItemSelectedListener(spinnerBusRouteHandler);
spinnerRoutes.setSelected(someSpinnerPosition, true);

// some other code that involves the second spinner

}

Do not expect the listener to be fired EXACTLY when you do the setSelected, because it wont. It will execute all the onCreate code, even the "// some other code that involves the second spinner" and only after that , the listener becomes active and is fired.

I encountered this problem myself and I had to find a workaround. Extremely annoying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM