0

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?

user3654055
  • 178
  • 14
  • Check these links 1. http://stackoverflow.com/questions/4923310/android-spinner-onitemselected-setonitemselectedlistener-not-triggering 2. http://stackoverflow.com/questions/16581536/setonitemselectedlistener-of-spinner-does-not-call to get some idea on implementing spinners. – Techidiot Sep 12 '15 at 20:17
  • Thanks. I'm looking at them and having some trouble still, but I am noticing that perhaps what I'm trying to do is not necessary. I want other Edit Text and Spinner fields to pop up depending on the option I choose. Am I wasting my time trying to get this working when I should be focusing on a different method? – user3654055 Sep 12 '15 at 21:02
  • Trying to do something which is not done is not a waste of time. Keep exploring but make sure you don't hold your work during this. – Techidiot Sep 12 '15 at 21:04
  • Yeah, I suppose I'm getting anxious. I like getting things done, and I like learning, but sometimes I find it hard to balance the two. I am looking at the second resource you sent me, and am having trouble implementing AT_AB's answer. Unfortunately it says I need 50 rep to comment on it, so I can't ask him about it. I'm going to change my question to include what I've tried so you can perhaps let me know what I'm missing? – user3654055 Sep 12 '15 at 21:14
  • Yes, indeed you are making it quite complicated that what it actually is. Its pretty simple. The first thumb rule to follow is, do not hard-code the array elements in your code. I ll post a code of mine an an answer. Check if that helps. – Techidiot Sep 12 '15 at 21:31
  • I would better put this link - http://stackoverflow.com/questions/28613245/how-to-get-the-value-of-a-selected-item-in-a-spinner It will help you for sure and will let you know "how to keep it simple" – Techidiot Sep 12 '15 at 21:39
  • Thank you so much! So what I'm getting from this is, the values that are shown or entered into the activity layout, regardless of Spinner, Edit Text, Check Box, etc, are meaningless, unless you save them into a local variable through the Java? From there you can manipulate events using the Java? – user3654055 Sep 12 '15 at 21:51
  • Correct. You are on your way ! :) – Techidiot Sep 12 '15 at 21:52
  • 1
    I think I'm even more on my way! So what I think I did wrong was, I was using the method correctly, (although in an over-complicated manner), but I was thinking about it incorrectly. Extends means that the class itself inherits every method from the Superclass, and super means "the class that this sub-class is extending". When I implement my OnItemSelectedListener, and override the OnItemSelected Class, OnItemSelectedListener stays the same except for the override, but it acts as an override as opposed to an extension. So SpinnerActivity = EditJobActivity in a way. sorry for length, to be c... – user3654055 Sep 12 '15 at 22:00
  • ontinued. So I'd like to chat, but I don't have the rep... Anyway, The error I was getting, that editJobActivity is not an enclosing class is true, because SpinnerActivity is an extension of the already existent Edit Job activity! So, first of all, how correct am I? I haven't taken java in a while, which is why this was such a struggle. Second, I want to try and keep things as organized as possible, so an entire class for All Spinners with case statements is optimal for me. However, is that in good coding practices? or should I keep it in EditJobActivity? – user3654055 Sep 12 '15 at 22:02
  • That's fine. +1 for your explanation. Let me know which link helped you the most and I will add that as an answer. – Techidiot Sep 12 '15 at 22:03
  • Honestly, the answer I just gave you didn't come from any one of these particularly. But it was 1) your persistence in having me do research, and 2 the general theme of all of them that helped. So I'd say put something like: Look in these 3 for help. Make sure you understand what Extending classes and Implementing classes are. You're overthinking things a bit. And if I have enough reputation to vote, you're getting the most I can give :) – user3654055 Sep 12 '15 at 22:07
  • Will see you soon! :) For now I am glad that you got the idea behind it. – Techidiot Sep 12 '15 at 22:13

1 Answers1

1

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!

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28