Hello I have the following object:
public class busObj {
private int id;
private String registrationNo;
private String model;
private String driver;
private String contact;
public busObj() { }
public busObj(int id, String registrationNo, String model, String driver, String contact) {
this.id = id;
this.registrationNo = registrationNo;
this.model = model;
this.driver = driver;
this.contact = contact;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRegistrationNo() {
return registrationNo;
}
public void setRegistrationNo(String registrationNo) {
this.registrationNo = registrationNo;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
@Override
public String toString() {
return model + " " + registrationNo + " - " + driver;
}
}
Now I have created an ArrayList of this object:
ArrayList<busObj> busList;
Which I then use to assign to my adapter and set my list view:
protected void setBusSpinner() {
bAdapter = new ArrayAdapter<busObj>(this, R.layout.simple_list_item, busList);
bAdapter.notifyDataSetChanged();
spBus.setAdapter(bAdapter);
}
Now the above code all works, however my issue is I have a list on that same activity (not a fragment) so when the user clicks an item in that list the data in the form should be populated.
I can do this with EditText and TextView's etc but when it come's to the Spinner I have no idea how to go about it since I have the actual item not the position.
In my case a selected item will be a "bankObj" but as you can see from my code I have overridden the "toString()" property to display the text instead of the object.
So supposing I have an object with the id of 1 how do I set that as the current selected item in my spinner?
I have seen several questions similar to this on stack overflow like: How to set selected item of Spinner by value, not by position? but none have worked for me so far.