Make the Tostring() method return two fields separately from a typed object

Asked

Viewed 120 times

0

I need to get the Tostring function to return the size and color separately, as I am filling a spinner with them. A spinner is filled with colors and another with size. However if I put:

public String ToString(){
  return cor + tamanho;
}

The two spinner are filled with color and size together...

Object:

public class Cores {
    private String cor;
    private String tamanho;

    public String getTamanho() {
        return tamanho;
    }

    public void setTamanho(String tamanho) {
        this.tamanho = tamanho;
    }

    public String getCor() {
        return cor;
    }

    public void setCor(String cor) {
        this.cor = cor;
    }

}
  • 4

    The ToString()not made for this, create a different method for your specific need, do not use a generic method that should already meet another need.

  • You have to implement an Adapter for each one. No override of the method getDropDownView() get and use the field you want to show. Related Custom Spinner showing Resource and non-text

1 answer

2

Reinforcing the @Maniero comment, you should create specific methods to get these properties separately. The standard getters methods should be sufficient to meet this need, see the code:

public class MeuObjeto {
  private String cor;
  private String tamanho;

  public String getCor() {
    return this.cor;
  }

  public String getTamanho() {
    return this.tamanho;
  }

  public String getCorTamanho() {
    return this.cor + " " + this.tamanho;
  }
}

The methods getCor() and getTamanho() return only the data according to the method name. Already the getCorTamanho() returns the concatenation of both, can be formatted as you wish.

  • In the first attempt, I used getCor(), as you are suggesting, but instead return me the value of the color (for example "red"). It returns the pack name... For example : com.example.example.exemplo.model.cliente.Colors@9e62....

  • @Daniel In this case edit the question and enter your class code MeuObjeto if you can

  • I placed the object there

  • If you’re getting the package name, it’s because cor is an object - and the default representation of an object in String, is its package name (with other information that is not the case now). Edit the question and add whole the code surrounding the colors, so we can help you more efficiently.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.