我从 SQLite 数据库中获取数据并将它们分配给数据对象。我正在使用自定义微调器适配器(实现SpinnerAdapter
)将它们放入微调器中,以便覆盖getItem()
andgetItemId()
方法。但是,android.R.layout.simple_spinner_dropdown_item
显示不正确(来自我的 Galaxy Tab 2 10.1 的屏幕截图):
相比之下,这是一个从数组中填充的微调器R.array
(正确的外观/大小):
通过此代码完成:
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);
我无法通过实现/扩展某种适配器来获得这种外观(我也尝试过扩展BaseAdapter
但无济于事)。这是我的微调器适配器的代码:
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;
}
}
任何帮助,将不胜感激。
编辑:我忘了提到这被称为使用
spnnrTerm.setAdapter(new MySpinnerAdapter(getActivity(), termList));