Spinner Android Retrofit

1

Good morning I'm creating an app, and I need it to have a spinner, which pulls all the data from a column of the database. But it turns out that when I pull it back the data but only that it makes the list view get into several instead of throwing all the data into a single spinner. I can only make it right when I put physical data in 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 one.

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 spin code and description.

I've tried doing this to pull, but it will not ...

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

But it does not work.

    
asked by anonymous 08.11.2017 / 13:50

1 answer

0

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

For this, I suggest creating a classe fault and override the ToString() method

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);

In order to obtain the% of selected%, you just have to make a spinner in cast of getSelectedItem()

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

Identificacao identificacao = item.getValue();
    
09.04.2018 / 22:43