Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom spinner adapter simple_spinner_dropdown_item not displaying correctly

I am getting data from an SQLite database and assigning them to data objects. I am using a custom spinner adapter (implements SpinnerAdapter) to put them in spinners so as to override the getItem() and getItemId() methods. However, the android.R.layout.simple_spinner_dropdown_item is not displaying correctly (screenshots from my Galaxy Tab 2 10.1):

Wrong spinner from data object

By contrast this is a spinner which is populated from an array in R.array (the correct look/size):

Proper spinner from R.array

Done by this code:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.quote_prices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

I am unable to get this look by implementing/extending some sort of adapter (I have also tried extending BaseAdapter to no avail). This is the code for my spinner adapter as it stands:

public class MySpinnerAdapter implements SpinnerAdapter {

    private Context context;
    /**
     * The internal data (the ArrayList with the Objects).
     */
    private List<? extends BaseDO> data;

    public MySpinnerAdapter(Context context, List<?extends BaseDO> data){
        this.context = context;
        this.data = data;
    }

    /**
     * Returns the Size of the ArrayList
     */
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getId();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
        }
        ((TextView) convertView).setText(data.get(position).toString());
        return convertView;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_item, null);
        textView.setText(data.get(position).toString());
        return textView;
    }


    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    public List<? extends BaseDO> getData() {
        return data;
    }

}

Any help would be appreciated.

EDIT: I forgot to mention that this is called using

spnnrTerm.setAdapter(new MySpinnerAdapter(getActivity(), termList));
like image 601
Rob Avatar asked Sep 02 '13 07:09

Rob


1 Answers

I know this question is old but I've been having the same issue and have just figured out the problem. After examining the source for ArrayAdapter.java I noticed it was passing the ViewGroup to the inflater and I wasn't. This was the problem all along.

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }
        ((TextView) convertView).setText(data.get(position).toString());
        return convertView;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_item, null);
        textView.setText(data.get(position).toString());
        return textView;
    }
like image 184
Steven Trigg Avatar answered Oct 23 '22 18:10

Steven Trigg