2

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.

Community
  • 1
  • 1
user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • I don't know if I got your question right but spinner has `onItemSelectedListener` interface which has `onItemSelected(AdapterView> parent, View arg1, int pos,long id)` which might help you. this gives the selected item position in the adapter. – Masked Man Oct 01 '16 at 21:38
  • No, the item is selected from a listview, the spinner is in the form in the same activity. So basically when the user taps on an item in the listview the spinner should change to the selected item's value. In my case the selected item is always an object. – user3718908x100 Oct 01 '16 at 21:52

1 Answers1

0

Have you tried getting bankObj.getId();

RStack
  • 230
  • 4
  • 16