Custom Spinner showing resource and not text

2

I'm having trouble setting up a custom spinner in my application. Here is an image of how it appears:

Followmycode:

Adapter.java

publicclassProfissionalCategoriaAdapterextendsArrayAdapter<ProfissionalCategoria>{privateContextcontext;privateArrayList<ProfissionalCategoria>lista;publicProfissionalCategoriaAdapter(Contextcontext,ArrayList<ProfissionalCategoria>lista){super(context,0,lista);this.context=context;this.lista=lista;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){finalProfissionalCategoriaitemPosicao=this.lista.get(position);convertView=LayoutInflater.from(this.context).inflate(R.layout.profissional_cat_item_spinner,null);finalViewlayout=convertView;TextViewtextView_categoriaNome=(TextView)convertView.findViewById(R.id.textView_profissionalCatItemSpinner);textView_categoriaNome.setText(itemPosicao.getNome());returnconvertView;}}

Activitywherethespinnershouldbeshown:

this.spinner_profissionalCategoria=(Spinner)myView.findViewById(R.id.spinner_profissionalCategoria);ArrayList<ProfissionalCategoria>lista=newDAOProfissionalCategoria(getActivity()).buscarCategoria(profissionalCategoria);ProfissionalCategoriaAdapterprofissionalCategoriaAdapter=newProfissionalCategoriaAdapter(getActivity(),lista);profissionalCategoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner_profissionalCategoria.setAdapter(profissionalCategoriaAdapter);spinner_profissionalCategoria.setPrompt("Escolha");

Professional Class Category

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 information you need is there.

Solution:

I created a method in DAO to fetch the data and return it 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 the 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's attention.

    
asked by anonymous 29.07.2016 / 13:51

1 answer

1

Short introduction to Spinner.

Spinner consists of two Views : the Selected item view , which shows the selected item and the drop-down view , where all items are shown and allows selection of one.

To get each Views , the Spinner class uses methods getView () and getDropDownView () , from the interface implementation SpinnerAdapter (usually a ArrayAdapter ) that is passed to you through the setAdapter ( ) .

The ArrayAdapter implements these two methods on the assumption that Views used have a TextView with id = android.R.id.text1 (in XML: android:id="@android:id/text1" ).
The TextView is populated with the result of calling the toString() method of each ArrayList object or passed to the constructor.

When these assumptions are not satisfied or can not be resolved using the ArrayAdapter (Context context, int resource, int textViewResourceId, List objects) , you need to create a custom adapter >. This adapter should be done override of these two methods in order to return correctly populated views.

Your issue.

Although you are using a custom adapter it does not override the getDropDownView() method. The adapter will thus have the default behavior that is to use the toString() method to get the text to display in the drop-down view .

As ProfessionalCategory does not override , the base class method ( Object ) is used to return a string consisting of the class, of which the object is an instance, the symbol @ , followed by the hexadecimal representation of the hash tag of the object .

How to resolve.

Use one of these 3 solutions.

  • @Override
    public String toString(){
        return nome;
    }
    
  • Override method toString() 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;
        }
    }
    

    You should delete the line:

    profissionalCategoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
  • As you only use a TextView , forget your adapter , do the override method getDropDownView() of the ProfessionalCategory class and use an 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
    Fill in a spinner with a field of an object

        
    29.07.2016 / 15:56