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):
By contrast this is a spinner which is populated from an array in R.array
(the correct look/size):
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));
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With