Spinner Android Retrofit

Asked

Viewed 161 times

1

Good morning I’m creating an app, and I need it to have a spinner, which pull all the data from a database column. But it turns out that when I pull it brings the dice, but only that it makes the list view stay in several, instead of throwing all the dice in one spinner. I can only make it right when I put physical data into the spinner.

spinner2 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter;
    List<String> list;
    list = new ArrayList<>();
    list.add(""); //Aqui eu coloco os dados fisicos... Eu quero puxar os dados de uma classe.
    adapter = new ArrayAdapter<String>(IdentificacaoActivity.this, 
android.R.layout.simple_spinner_dropdown_item, list);
    spinner2.setAdapter(adapter);

The class I want to pull is this.

public class Identificacao {
private String cnpjcpf;
private String codigocondicao;
private String prazoentrega;
private String observacoes;
private String cnpjloja;
private String senhaacesso;
private String dataacesso;
private String horaacesso;
private String descricao;

public String getDescricao() {
    return descricao;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}

public String getCnpjcpf() {
    return cnpjcpf;
}

public void setCnpjcpf(String cnpjcpf) {
    this.cnpjcpf = cnpjcpf;
}

public String getCodigocondicao() {
    return codigocondicao;
}

public void setCodigocondicao(String codigocondicao) {
    this.codigocondicao = codigocondicao;
}

public String getPrazoentrega() {
    return prazoentrega;
}

public void setPrazoentrega(String prazoentrega) {
    this.prazoentrega = prazoentrega;
}

public String getObservacoes() {
    return observacoes;
}

public void setObservacoes(String observacoes) {
    this.observacoes = observacoes;
}

public String getCnpjloja() {
    return cnpjloja;
}

public void setCnpjloja(String cnpjloja) {
    this.cnpjloja = cnpjloja;
}

public String getSenhaacesso() {
    return senhaacesso;
}

public void setSenhaacesso(String senhaacesso) {
    this.senhaacesso = senhaacesso;
}

public String getDataacesso() {
    return dataacesso;
}

public void setDataacesso(String dataacesso) {
    this.dataacesso = dataacesso;
}

public String getHoraacesso() {
    return horaacesso;
}

public void setHoraacesso(String horaacesso) {
    this.horaacesso = horaacesso;
}
}

I need to pull the code and description.

I’ve tried it to pull, but it won’t...

Identificacao item = new Identificacao();
list.add(item.getCodigoCondicao +" "+item.getDescricao);

But it doesn’t work.

1 answer

1

You can override the method ToString() of your class Identificacao, and and create a adapter of the kind List<Identificacao> instead of the kind List<String>, but this does not leave the design flexible in my opinion..

For this, I suggest creating a classe assist and overwrite the method ToString()

public class ItemData<T> {

    private String mText = "";
    private T mValue = null;

    public ItemData(String text, T value) {
        this.mText = text;
        this.mValue = value;
    }

    public String getText() {
        return mText;
    }

    public void setText(String mText) {
        this.mText = mText;
    }

    public T getValue() {
        return mValue;
    }

    public void setValue(T mValue) {
        this.mValue = mValue;
    }


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

In your code just do:

List<ItemData<Identificacao>> mLista  = new List<ItemData<Identificacao>>();

Identificacao item = new Identificacao();
//puxar os dados
ItemData itemData = new ItemData<Identificacao>(item.getCodigoCondicao +" "+item.getDescricao, item);
mLista.add(itemData);


mDataAdapter =
            new ArrayAdapter<ItemData<Identificacao>>(this.mContext,
                    android.R.layout.simple_spinner_item,
                    mLista);
mDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mDataAdapter);

To obtain the ENTITY of the spinner selected just make a cast in the getSelectedItem() of spinner

ItemData<Identificacao> item = ((ItemData<Identificacao>)spinner.getSelectedItem());

Identificacao identificacao = item.getValue();

Browser other questions tagged

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