user3654055
user3654055

Reputation: 178

Trouble adding a Spinner in Android Studio

KEEPING FOR HISTORIC. SKIP TO EDIT.

I am having trouble adding a spinner to an android app I'm developing. I haven't developed the code yet to go in the app, but just to do some testing I have it sending a toast message to let me know it works. According to this page: http://developer.android.com/guide/topics/ui/controls/spinner.html You can use User to create events by having OnItemSelected be called in another class.

public class SpinnerActivity extends EditJobActivity implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
        Toast.makeText(SpinnerActivity.this, "It worked", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

That's it's own class. I call it with this:

    //Prepare the first (Job Discovery) spinner
    Spinner mJobDiscovery = (Spinner) findViewById(R.id.SpinJobDiscovered);
    // Create an ArrayAdapter using the string array and a default spinner layout
    JobDiscoveryAdapter = ArrayAdapter.createFromResource(this,
            R.array.spin_JobDiscoveryHome, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    JobDiscoveryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    mJobDiscovery.setAdapter(JobDiscoveryAdapter);
    mJobDiscovery.setOnItemSelectedListener(this);

However, I get this error: SetOnItemSelectedListener(android...) in AdapterView cannot be applied to (com...Activity)

It asks me to cast it to (AdapterView.OnItemSelectedListener) but when I do I get errors because I can't cast the activity to an OnItemSelectedListener. What am I missing here? I'm a bit new to Android Programming, so I'm sorry if this is an easy answer...

EDIT:

After speaking with Bhush_techidiot, he sent me to some resources that helped, however I'm having trouble finalizing my implementation. Now my SpinnerActivity temporarily looks like this:

public class SpinnerActivity extends EditJobActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_job);

        /*for fill your Spinner*/
    List<String> SpinnerArray = new ArrayList<String>();
    SpinnerArray.add("Item 1");
    SpinnerArray.add("Item 2");
    SpinnerArray.add("Item 3");
    SpinnerArray.add("Item 4");
    SpinnerArray.add("Item 5");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, SpinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner spinner = (Spinner) findViewById(R.id.SpinJobDiscovered);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            // TODO Auto-generated method stub
            Object item = arg0.getItemAtPosition(arg2);
            if (item != null) {
                Toast.makeText(EditJobActivity.this, item.toString(),
                        Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(EditJobActivity.this, "Selected",
                    Toast.LENGTH_SHORT).show();

        }

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

        }
    });
}
}

but I don't know how to call SpinnerActivity from my EditJobActivity, so I'm getting the error: "... is not an enclosing class" on EditJobActivity. Should I be making a new layout for this spinner?

Upvotes: 0

Views: 1310

Answers (1)

Techidiot
Techidiot

Reputation: 1947

Check the following links. Make sure you keep your solutions as simple as you can also don't hesitate to try complicated things once you get the simple one working :) Understand the extending and implementing classes and you are good to go!

  1. Android Spinner - onItemSelected / setOnItemSelectedListener not triggering
  2. setOnItemSelectedListener of Spinner does not call
  3. How to get the value of a selected item in a spinner?

All the best!

Upvotes: 1

Related Questions