2

I am wanting to create a Spinner from an ArrayList of Objects I have created and therefore when one is selected I can go back to that ArrayList and get the rest of the information from it

Example

public class ObjectName {
    private int ID;
    private String name;
    private String name2;
    public ObjectName{int pID, String pName, String pName2) {
        ID = pID;
        name = pName;
        name2 = pName2;
    }
    //Getters Here
}

Example of Spinner Code

ArrayList<ObjectName> objects = new ArrayList<ObjectName>;
ArrayAdapter<ObjectName> adapter = new ArrayAdapter<ObjectName>(this, android.R.layout.simple_spinner_item, objects);

Of course the Spinner does not show what I want. Is there an easy way to solve this

Thanks for your time

2
  • Where are you adding your values to ArrayList? Commented Oct 31, 2011 at 15:20
  • Creating the Arraylist from another class (pulling data from database) Commented Oct 31, 2011 at 15:25

2 Answers 2

2

You need to implement toString method inside ObjectName class. Otherwise spinner cannot know what to show!

public String toString() {
    return ID + " " + name + " " + name2;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Do you need to change the ArrayAdapter Line?
0

You need to implement toString in your ObjectName class as Caner said, and you will also need to set the adapter for your spinner, using something like:

((Spinner) findViewById(R.id.mySpinner)).setAdapter(adapter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.