我可以创建自己的.toString()函数吗?

时间:2018-11-01 15:23:02

标签: java android xml

如何在vigiltime.setText中显示对象的另一个字段?我希望它显示来自parishArrayList对象的时间字段的特定相关值吗? parent.getItemAtPosition(position)已经检索到特定的对象,然后如何在onItemSelected方法中获取它以解析相关的对象详细信息?

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

}

4 个答案:

答案 0 :(得分:1)

您不需要使用toString()。您可以简单地调用教区类的相关函数或变量:

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

如果您只想使用toString(),请在教区类中将其覆盖:

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

编辑:回答您的实际问题:

ViewAdapter#getItemAtPosition()返回一个Object,而不是您的特定类。您需要将该呼叫发送给教区:

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

然后您可以致电item.getVigilTimes();

答案 1 :(得分:0)

在要更改字符串表示形式的类中重写toString方法。

@Override
public String toString() {
    //TODO - Here.
}

答案 2 :(得分:0)

您可以通过下面显示的类的公共方法访问对象的任何私有变量。

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

您还可以重写toString()方法以显示VigilTimes并调用parent.getItemAtPosition(position).toString()。

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

答案 3 :(得分:0)

toString()方法是从Object类继承的,而Object类是Java中其他所有类的继承对象。基本的toString()返回this:     getClass()。getName()+'@'+ Integer.toHexString(hashCode())

字符串类,如果要创建一个新的字符串对象,例如String

first_name = 'someFirstName' 

实际上使用构造函数

创建一个实例
String first_name = new String("someFristName") 

并且此类再次重写对象toString()方法。

Oracle的文档中提到了String类中的toString(), 该对象(已经是一个字符串!)本身会返回。

创建或构建的每个类都直接或间接地继承自Object类,而Object类具有可在当前类中覆盖的基础toString()。就这么简单...

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

您重写的toString()只不过是您的getName()方法。考虑是否有必要。