android – 自定义微调器适配器simple_spinner_dropdown_item无法正确显示
作者:互联网
我从SQLite数据库获取数据并将它们分配给数据对象.我使用自定义微调器适配器(实现SpinnerAdapter)将它们放在微调器中,以覆盖getItem()和getItemId()方法.但是,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));
解决方法:
我知道这个问题已经过时但我遇到了同样的问题并且刚刚发现了问题.在检查了ArrayAdapter.java的源代码之后,我注意到它将ViewGroup传递给了inflater,而我却没有.这一直是个问题.
@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;
}
标签:android,android-layout,android-spinner 来源: https://codeday.me/bug/20191006/1861533.html