Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know the thread is old, but just in case...</p> <p>User object:</p> <pre><code>public class User{ private int _id; private String _name; public User(){ this._id = 0; this._name = ""; } public void setId(int id){ this._id = id; } public int getId(){ return this._id; } public void setName(String name){ this._name = name; } public String getName(){ return this._name; } } </code></pre> <p>Custom Spinner Adapter (ArrayAdapter)</p> <pre><code>public class SpinAdapter extends ArrayAdapter&lt;User&gt;{ // Your sent context private Context context; // Your custom values for the spinner (User) private User[] values; public SpinAdapter(Context context, int textViewResourceId, User[] values) { super(context, textViewResourceId, values); this.context = context; this.values = values; } @Override public int getCount(){ return values.length; } @Override public User getItem(int position){ return values[position]; } @Override public long getItemId(int position){ return position; } // And the "magic" goes here // This is for the "passive" state of the spinner @Override public View getView(int position, View convertView, ViewGroup parent) { // I created a dynamic TextView here, but you can reference your own custom layout for each spinner item TextView label = (TextView) super.getView(position, convertView, parent); label.setTextColor(Color.BLACK); // Then you can get the current item using the values array (Users array) and the current position // You can NOW reference each method you has created in your bean object (User class) label.setText(values[position].getName()); // And finally return your dynamic (or custom) view for each spinner item return label; } // And here is when the "chooser" is popped up // Normally is the same view, but you can customize it if you want @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { TextView label = (TextView) super.getDropDownView(position, convertView, parent); label.setTextColor(Color.BLACK); label.setText(values[position].getName()); return label; } } </code></pre> <p>And the implementarion:</p> <pre><code>public class Main extends Activity { // You spinner view private Spinner mySpinner; // Custom Spinner adapter (ArrayAdapter&lt;User&gt;) // You can define as a private to use it in the all class // This is the object that is going to do the "magic" private SpinAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create the Users array // You can get this retrieving from an external source User[] users = new User[2]; users[0] = new User(); users[0].setId(1); users[0].setName("Joaquin"); users[1] = new User(); users[1].setId(2); users[1].setName("Alberto"); // Initialize the adapter sending the current context // Send the simple_spinner_item layout // And finally send the Users array (Your data) adapter = new SpinAdapter(Main.this, android.R.layout.simple_spinner_item, users); mySpinner = (Spinner) findViewById(R.id.miSpinner); mySpinner.setAdapter(adapter); // Set the custom adapter to the spinner // You can create an anonymous listener to handle the event when is selected an spinner item mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int position, long id) { // Here you get the current item (a User object) that is selected by its position User user = adapter.getItem(position); // Here you can do the action you want to... Toast.makeText(Main.this, "ID: " + user.getId() + "\nName: " + user.getName(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView&lt;?&gt; adapter) { } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload