1

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.

Marcelo Luz
  • 452
  • 3
  • 8
  • 2
    Please define "just doesnt seem to work". – kosa Nov 15 '12 at 22:16
  • crap i just realized i posted the wrong activity so it wouldnt make sense, is there anyway to edit the qestion or delete it? – Marcelo Luz Nov 15 '12 at 22:23
  • 1
    You should be able to edit your question, aren't you seeing any edit links? – kosa Nov 15 '12 at 22:24
  • found it thanks, ok theres the right class, ok so by doesnt seem to work i mean absolutely nothing happens when any item is selected, at first i had an "if" so it would only listen when one item was selected but i took that out to test it out – Marcelo Luz Nov 15 '12 at 22:26
  • Have you checked logcat to see it is logging those entries you have in code? – kosa Nov 15 '12 at 22:27
  • 1
    Does your `westbank_stops` array have any data in it? – Sam Nov 15 '12 at 22:28
  • yes, just some filler numbers – Marcelo Luz Nov 15 '12 at 22:31
  • Wait, does `spinnerRoutes` show any data? You haven't set any adapters in `onCreate()`, maybe you used `android:entries` in your XML but I want to double check... – Sam Nov 15 '12 at 22:32
  • yeah it does, sorry forgot to mention that the data for spinnerRoutes is set in the XML activityLayout file – Marcelo Luz Nov 15 '12 at 22:33
  • nope, figured it out though seems like i had 2 spinners with similar name, was pulling the one from the wrong activity. Thanks a lot guys, sorry to waste your times!!! – Marcelo Luz Nov 15 '12 at 22:38
  • I have found [this answer][1] for you. Hope this helps you in your research :) [1]: http://stackoverflow.com/a/1714426/1116216 – Michele La Ferla Jun 15 '14 at 09:24

1 Answers1

1

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.

user1137313
  • 2,390
  • 9
  • 44
  • 91