0

The code is:

public class Organization {

private String name;
private Long id;

public Organization(){
}

public Organization(String name, Long id) {
    super();
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
}

The spinner is:

 Spinner sp = (Spinner) navigationView.getMenu().findItem(R.id.brand_spinner).getActionView();
 sp.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,contactList));
 sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String value = parent.getItemAtPosition(position).toString();
            Toast.makeText(parent.getContext(),""+value+"",Toast.LENGTH_SHORT).show();
        }
 });

How can I pass both name and id to the spinner and the should be listed in the spinner, by selecting the name in the spinner. Store the id of the name in the local variable.

JulienD
  • 7,102
  • 9
  • 50
  • 84
Vaishak Babu
  • 53
  • 1
  • 8

2 Answers2

0

You have to create your own Custom Adapter class which will extend BaseAdapter or ArrayAdapter and in that you just pass the ArrayList<> of your Organization.

Just check this link it will help you in creating adapter

Check Link

Jaymin Panchal
  • 2,797
  • 2
  • 27
  • 31
0

For ArrayAdapter you are sending contactList, override toString() in objects of contactList. So ArrayAdapter while rendering it will call toString() method each object of contactList to show the text in textView.

So now onClick of item,

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
        @Override 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Object obj= parent.getItemAtPosition(position);
            Contact c = (Contact) obj;
            System.out.println("id = " + c.getId() + " , Name = " + c.getName());

 } 
Abilash
  • 218
  • 3
  • 10