-2

How can I display another field from my object in vigiltime.setText? I want it to display the specific relating value of the time fields wihtin the object from the parishArrayList? The parent.getItemAtPosition(position) already retrieves the specific object then how can I get it to parse relevant object details within the onItemSelected method?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    parishArrayList = new ArrayList<>();
    parishArrayList.add(new Parish(1, "Aghyaran", "Termonamongan, N.West Tyrone", "6.30pm", "10.00am"));
    parishArrayList.add(new Parish(2, "Castlederg", "Castlederg, N.West Tyrone", "7pm", "11.00am"));
    parishArrayList.add(new Parish(3, "Strabane", "Strabane, N.West Tyrone", "8pm", "12.00am"));

    Spinner parishSpinner = (Spinner) findViewById(R.id.spinner);

    // Create an ArrayAdapter using the parishArrayList and a default spinner layout
    ArrayAdapter<Parish> parishAdapter = new ArrayAdapter<Parish>(getApplicationContext(), android.R.layout.simple_spinner_item, parishArrayList);

    // Specify the layout to use when the list of choices appears
    parishAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the adapter to the spinner
    parishSpinner.setAdapter(parishAdapter);

    parishSpinner.setOnItemSelectedListener(this);
}
@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TextView vigiltime = (TextView) findViewById(R.id.vigiltime);
    vigiltime.setText("Spinner selected : ");
    vigiltime.setText(vigiltime.getText() + parent.getItemAtPosition(position).toString());

}

Parish.java

public class Parish {

private int parishIdNumber;
private String pName;
private String pAddress;
private String pVigilTimes;
private String pSundayTimes;

public Parish(int id, String name, String address, String vigilTimes, String sundayTimes) {
    parishIdNumber = id;
    pName = name;
    pAddress = address;
    pVigilTimes = vigilTimes;
    pSundayTimes = sundayTimes;
}

public int getId() {
    return parishIdNumber;
}

public void setId(int id) {
    parishIdNumber = id;
}

public String getName() {
    return pName;
}

public void setName(String name) {
    pName = name;
}

public String getAddress() {
    return pAddress;
}

public void setAddress(String address) {
    pAddress = address;
}

public String getVigilTimes() {
    return pVigilTimes;
}

public String getsundayTimes() {
    return pSundayTimes;
}

public int getParishIdNumber() {
    return parishIdNumber;
}

public void setParishIdNumber(int parishIdNumber) {
    this.parishIdNumber = parishIdNumber;
}

@Override
public String toString() {
    return pName;
}

}

oisinmc
  • 31
  • 1
  • 7
  • Possible duplicate of [How to override toString() properly in Java?](https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java) – Chris Stillwell Nov 01 '18 at 15:28

4 Answers4

1

You don't need to use toString(). You could simply call the relevant functions or variables of your Parish class:

Parish item = parent.getItemAtPosition(position);
vigilTime.setText("Spinner selected : ");
vigilTime.append(item.getTime() + " "); //append has the same effect as what you're currently doing
vigilTime.append(item.getSomethingElse + " ");
//etc

If you want to simply use toString(), override it in your Parish class:

@Override 
public String toString() {
    return /* format the String you want returned here */
}

EDIT: to answer your actual question:

ViewAdapter#getItemAtPosition() returns an Object, not your specific class. You need to cast that call to Parish:

Parish item = (Parish) parent.getItemAtPosition(position);

Then you can call item.getVigilTimes();.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
0

Override the toString method in the class that you want to alter the string representation.

@Override
public String toString() {
    //TODO - Here.
}
adickinson
  • 553
  • 1
  • 3
  • 14
  • I have already done that but I want it to display a different value than what is shown in the spinner dropdown, i only want it to show a time field from the relevant object, not the object name itself – oisinmc Nov 01 '18 at 15:28
  • Then that's not the toString method that you want, you've already written your own toString method for the Parish class. Get the Parish object at the selected position, and then output the result of getVigilTimes() in the text field. – adickinson Nov 01 '18 at 15:37
0

You can access any private variable of an object through a public method of class shown below.

vigiltime.setText(vigiltime.getText() + ((Parish)parent.getItemAtPosition(position)).getVigilTimes());

You can also override toString() method to display VigilTimes and call parent.getItemAtPosition(position).toString().

@Override
public String toString() {
    return pVigilTimes;
}
Joy
  • 394
  • 2
  • 9
0

The toString() method is inherited from the Object Class that every other class in java inherits from. The foundational toString() returns this: getClass().getName() + '@' + Integer.toHexString(hashCode())

The string class if you are creating a new string object like String

first_name = 'someFirstName' 

actually creates an instances with the constructor,

String first_name = new String("someFristName") 

and this class overrides the object toString() method once more.

The documentation at Oracle says of toString() in the String class, This object (which is already a string!) is itself returned.

Every single class that is created or built is directly or indirectly inherited from the Object class which has the foundational toString() which one can override within the current class. It's as simple as...

 @Override
 public String toString(){
      //to do logic here
 }

Your overrided toString() is nothing more than your getName() method. Consider if it is necessary.