Spinner in Popup

Asked

Viewed 167 times

2

I have a screen where it has a Gridview and a ImageButton.

Well, by clicking on that ImageButton open a PopUp containing two spinner and a ImageButton.

Those spinners are populated through a web service that is called after the user logs in, so far so good.

When I select the information in both spinners and I click on button I can’t recover the values (description or id) I populated with my adapter where, if I use the same code outside a Popup, I can retrieve the information but when I use it in a popup.

Follow my Adapter code for my spinner

package br.com.projeto.adapter;


import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.projeto.entidade.DadosFiliais;
import br.com.projeto.entidade.DadosPortifolio;
import br.com.projeto.osportifolio.R;

public class SpinnerAdapter extends BaseAdapter {
    private Context context;
    ArrayList<DadosFiliais> data = new ArrayList<DadosFiliais>();
    public SpinnerAdapter(Context context,ArrayList<DadosFiliais> data) {
        this.context = context;
        this.data = data;
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

    DadosFiliais datas = new DadosFiliais(); 
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(android.R.layout.simple_list_item_1, null);
    TextView text1 = (TextView) view.findViewById(android.R.id.text1);                      
    text1.setText(data.get(position).getDsFilial());
    return view;
}
@Override
public int getCount() {
    return 32;
}
@Override
public Object getItem(int position) {
    return  "TextView";
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.row_spinner, null);

    TextView text1 = (TextView) view.findViewById(R.row_s.dados_spinner);
    TextView text2 = (TextView) view.findViewById(R.row_s.dados_spinner_2);

    text1.setText(data.get(position).getDsFilial());
    text2.setText(data.get(position).getCdFilial());
    return view;
}

}

Code used to pick up spinner values

String localidadeStr = txtLocalidade.getText().toString();
String movimentoStr = txtMovimento.getText().toString();
localidade = Integer.parseInt(localidadeStr); 
movimento = Integer.parseInt(movimentoStr);

instead of bringing the value it recovers the value "Textview"

1 answer

1

I believe that String "TextView" that is returned from the method getItem of your Adapter is the problem while recovering the selected item.

Change your Adapter to:

public class SpinnerAdapter extends BaseAdapter {
    private Context context;
    ArrayList<DadosFiliais> data = new ArrayList<DadosFiliais>();

    public SpinnerAdapter(Context context,ArrayList<DadosFiliais> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        // Porque instanciar um DadosFilias?
        //DadosFiliais datas = new DadosFiliais(); 
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(android.R.layout.simple_list_item_1, null);
        TextView text1 = (TextView) view.findViewById(android.R.id.text1);                      
        text1.setText(data.get(position).getDsFilial());

        return view;
    }

    @Override
    public int getCount() {
        //return 32;
        // Mude para retornar a quantidade de itens da lista.
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        //return "TextView";
        // Em vez de retornar a String "TextView" fixa, retorna o dado da lista
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {     
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.row_spinner, null);

        TextView text1 = (TextView) view.findViewById(R.row_s.dados_spinner);
        TextView text2 = (TextView) view.findViewById(R.row_s.dados_spinner_2);

        text1.setText(data.get(position).getDsFilial());
        text2.setText(data.get(position).getCdFilial());

        return view;
    }
}

To access the object DadosFiliais selected at Spinner:

// Recupera seu spinner
Spinner sp = (...);

// Recupera o objeto selecionado direto do Adapter
DadosFilias objetoSelecionado = sp.getSelectedItem();

How your Adapter does much access to findViewById, imagine a very long list, go through the tree of Views makes the listing slow. For this I recommend looking at the pattern View Holder

Browser other questions tagged

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