Custom Spinner showing Resource and non-text

Asked

Viewed 341 times

2

I’m having trouble setting up a custom spinner in my application. Follow below picture of how it appears:

inserir a descrição da imagem aqui

Follows my code:

Java adapter.

public class ProfissionalCategoriaAdapter extends ArrayAdapter<ProfissionalCategoria>   {

    private Context context;
    private ArrayList<ProfissionalCategoria> lista;

    public ProfissionalCategoriaAdapter(Context context, ArrayList<ProfissionalCategoria> lista) {
        super(context, 0, lista);
        this.context = context;
        this.lista = lista;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ProfissionalCategoria itemPosicao = this.lista.get(position);

        convertView = LayoutInflater.from(this.context).inflate(R.layout.profissional_cat_item_spinner,null);
        final View layout = convertView;

        TextView textView_categoriaNome = (TextView) convertView.findViewById(R.id.textView_profissionalCatItemSpinner);
        textView_categoriaNome.setText(itemPosicao.getNome());

        return convertView;
    }
}

Activity where the spinner should be shown:

this.spinner_profissionalCategoria = (Spinner) myView.findViewById(R.id.spinner_profissionalCategoria);

ArrayList<ProfissionalCategoria> lista = new DAOProfissionalCategoria(getActivity()).buscarCategoria(profissionalCategoria);
ProfissionalCategoriaAdapter profissionalCategoriaAdapter = new ProfissionalCategoriaAdapter(getActivity(),lista);
profissionalCategoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner_profissionalCategoria.setAdapter(profissionalCategoriaAdapter);
spinner_profissionalCategoria.setPrompt("Escolha");

Classe Professionalcategoria

public class ProfissionalCategoria { 
  private int autoId; 
  private String nome; 

  public int getAutoId() { 
    return autoId; 
  }

  public void setAutoId(int autoId) { 
    this.autoId = autoId; 
  }

  public String getNome() {
    return nome; 
  } 

  public void setNome(String nome) { 
    this.nome = nome; 
  }
}

I believe the necessary information is there.

Solution:

I created a method in DAO to fetch the data and return with a different type, see:

Previous method:

public ArrayList<ProfissionalCategoria> buscarCategoria(ProfissionalCategoria profissionalCategoria) {
    ArrayList<ProfissionalCategoria> lista = new ArrayList<>();
    String[] colunas = new String[]{"autoid", "nome"};

    Cursor cursor = this.db.query("ProfissionalCategoria",colunas,null,null,null,null,"nome");

    if(cursor.moveToFirst()){
        do {
            profissionalCategoria = new ProfissionalCategoria();
            profissionalCategoria.setAutoId(cursor.getInt(0));
            profissionalCategoria.setNome(cursor.getString(1));

            lista.add(profissionalCategoria);
        } while (cursor.moveToNext());
        cursor.close();

    }
    db.close();
    return lista;
}

New method:

public List<String> buscarCatSpinner(){
    List<String> list = new ArrayList<String>();
    String[] colunas = new String[]{"autoid", "nome"};
    Cursor cursor = this.db.query("ProfissionalCategoria",colunas,null,null,null,null,"nome");

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            list.add(cursor.getString(1));//adding 2nd column data
        } while (cursor.moveToNext());
    }
    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return list;
}

And in Activity where it shows the spinner:

DAOProfissionalCategoria db = new DAOProfissionalCategoria(getActivity());
    List<String> labels = db.buscarCatSpinner();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, labels);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_profissionalCategoria.setAdapter(dataAdapter);

Thanks for the help everyone attention.

  • In fact it seems to be showing the object name, not the property value returned by itemPosicao.getName(). Try to put a Breakpoint and debug there the Getview function being loaded in itemPosition.

  • Hello Celso, then at breakpoint it shows the following: List: size=4, position=0 .

  • David, if you have found a solution other than the answers, you should publish it as an answer rather than adding it to the question, so your solution will be more evident.

1 answer

2


Short introduction to Spinner.

The Spinner is composed of two Views: to Selected item view, that shows the selected item and the drop-down view, where all items are shown and which allows the selection of one of them.

To obtain each of the Views, the class Spinner uses the methods getView() and getDropDownView(), implementation of the interface Spinneradapter(usually a Arrayadapter) which is passed to you by the method setAdapter().

The Arrayadapter implements these two methods on the assumption that the Views used have a Textview with id = android.R.id.text1(in XML:android:id="@android:id/text1").
The Textview is filled with the result of the call to the method toString() of each object of array or Arraylist passed to the builder.

When these assumptions are not met or cannot be resolved using the constructor Arrayadapter(Context context, int Resource, int textViewResourceId, List Objects), it is necessary to create a custom Adapter.
In that Adapter should be done the override of these two methods in order to return the views correctly filled.

Your problem.

Although I’m using a custom Adapter he doesn’t do the override of the method getDropDownView().
The Adapter will thus have the default behavior of using the method toString() to obtain the text to be displayed in drop-down view.

Like Professionalism doesn’t do his override, the base class method is used(Object) that returns a string composed by the class name, of which the object is an instance, the symbol @, followed by the hexadecimal representation of the code hash of object.

How to solve.

Use one of these 3 solutions.

  1. Keep up your Adapter how are and do the override of the method toString() class Professionalism in this way:

    @Override
    public String toString(){
        return nome;
    }
    
  2. Do the override of the method getDropDownView() of your Adapter

    public class ProfissionalCategoriaAdapter extends ArrayAdapter<ProfissionalCategoria>   {
    
        private Context context;
        private ArrayList<ProfissionalCategoria> lista;
    
        public ProfissionalCategoriaAdapter(Context context, ArrayList<ProfissionalCategoria> lista) {
            super(context, 0, lista);
            this.context = context;
            this.lista = lista;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            return getCustomView(position, convertView, parent)
        }
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent){
            return getCustomView(position, convertView, parent)
        }
    
        //Método auxiliar para criar a view, já que se usa a mesma view nos dois métodos
        private View getCustomView(int position, View convertView, ViewGroup parent){
            final ProfissionalCategoria itemPosicao = this.lista.get(position);
    
            convertView = LayoutInflater.from(this.context).inflate(R.layout.profissional_cat_item_spinner,null);
            final View layout = convertView;
    
            TextView textView_categoriaNome = (TextView) convertView.findViewById(R.id.textView_profissionalCatItemSpinner);
            textView_categoriaNome.setText(itemPosicao.getNome());
    
            return convertView;
        }
    }
    

    Must delete the line:

    profissionalCategoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
  3. How do you use a View only with a Textview, forget your Adapter, do the override of the method toString() class Professionalism and use a Arrayadapter

    ArrayList<ProfissionalCategoria> lista = new DAOProfissionalCategoria(getActivity()).buscarCategoria(profissionalCategoria);
    ArrayAdapter<ProfissionalCategoria> dataAdapter = new ArrayAdapter<ProfissionalCategoria>(getActivity(),android.R.layout.simple_spinner_item, lista);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_profissionalCategoria.setAdapter(dataAdapter);
    

Related questions:

Spinner to select Color
With filling a spinner with an object field

Browser other questions tagged

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