2017-01-11 33 views
0

的代碼是:如何將對象傳遞給android中的微調器?

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; 
} 
} 

的微調是:

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(); 
     } 
}); 

我怎麼能同時通過姓名和身份證到微調和應微調上市,通過在選擇名稱微調。將名稱的ID存儲在本地變量中。

+0

http://stackoverflow.com/questions/1625249/android-how-to-bind-spinner-to-custom-object-list –

+0

定義適配器是必需的爲了這。 –

+0

http://stackoverflow.com/questions/35983176/how-to-create-spinner-list-using-customadapter-in-android –

回答

0

您必須創建您自己的自定義適配器類,它將擴展BaseAdapterArrayAdapter,並且您只需通過OrganizationArrayList<>即可。

只是檢查這個鏈接它會幫助你創建適配器

Check Link

0

對於ArrayAdapter你在contactList的對象發送contactList,覆蓋的toString()。因此,ArrayAdapter在渲染時會調用toString()方法contactList的每個對象來顯示textView中的文本。

所以,現在項目的onClick,

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()); 

}