Spinner in Popup

2

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

Well, by clicking on this ImageButton I open a PopUp that contains two spinner and a ImageButton .

These 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 the two spinners and click on the button , I can not retrieve the values (description or id) that I populated with my adapter being that if I use the same code outside of a PopUp, the information but when I use it in a popup does not.

Follow the code on my adapter 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 retrieves the value "TextView"

    
asked by anonymous 26.08.2014 / 22:16

1 answer

1

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

Switch 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 DadosFiliais object selected in Spinner:

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

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

As your adapter makes a lot of access to findViewById , imagine a very large list, scrolling through the Views tree makes the listing slow. For this I recommend that you look at the default View Holder

    
27.08.2014 / 03:37