0

My Spinner is stuck, I can't seem to get it to retrieve the selected item. It is stuck on the first item of the string-array. I want the user to choose item then place markers on the map based on their selections. I don't want to predetermine the item and retrieve so I am using:

spinner.getSelectedItem().toString();

It should be simple and straight forward as that. Without having to code for each item in string-array. using .trim().equals(""). The biutton uses android:onClick and not setOnClickListener().

This is my code:

onMapReady() calls this method:

public void talktomap(){
//variables have been declared arleady
SP_course = (Spinner)findViewById(R.id.select_subject);
course= SP_course.getSelectedItem().toString();

    //talk to map
    if (loc=="Sydney" && level=="Form 3" && course=="Biology"){
        map.addMarker(new MarkerOptions().position(MAU).title("Biology").snippet("You found us").alpha(0.7f).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        map.addMarker(new MarkerOptions().position(PortLoius1).title("Biology").snippet("..add your description here..").alpha(0.7f).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

        map.addMarker(new MarkerOptions().position(new LatLng(-20.353, 57.65)).title("...").snippet("Biology Tutor Here").alpha(0.7f));

    }else  if (loc=="melbourne" && level=="Form 4" && course=="English"){
        map.addMarker(new MarkerOptions().position(new LatLng(-20.2012, 57.6)).title("English").snippet("English Tutor Here").alpha(0.7f).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
        map.addMarker(new MarkerOptions().position(new LatLng(-20.2034, 57.6)).title("English").snippet("English Tutor Here").alpha(0.7f));

    }else if (loc=="new york" && level=="Form 3" && course=="Chemistry"){
        map.addMarker(new MarkerOptions().position(BonneTerre).title("Chemistry").snippet("Chemistry Tutor Here").alpha(0.7f).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
        map.addMarker(new MarkerOptions().position(BonneTerre1).title("Chemistry").snippet("Chemistry Tutor Here").alpha(0.7f).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

    }else {
        Toast.makeText(this,"Sorry. No results found",Toast.LENGTH_LONG).show();
    }
}

When the app starts it just displays the toast meaning it can enter the condition. When I choose different items and press the button the app crashes.

The string-array is stuck on 'Choose subject'

<string-array name="select_subject">
    <item>-- Choose subject --</item>
    <item>Biology</item>
    <item>Chemistry</item>
    <item>English</item>
    <item>French</item>
    <item>Mathematics</item>
</string-array>
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3650467
  • 173
  • 1
  • 1
  • 9

1 Answers1

0

Check how you initialize SP_course variable.

Upon reading the documentation of Google, here is their sample code.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

Also, a code snippet from a related SO question.

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

Hope this helps!

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91