我有以下代码,在Spinner上设置字体类型
private class MySpinnerAdapter extends ArrayAdapter<CharSequence> {
private Context context;
public MySpinnerAdapter(Context context, int textViewResourceId,
List<CharSequence> objects) {
super(context, textViewResourceId, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
TextView item = (TextView) convertView.findViewById(R.id.item);
FontUtils.setRobotoFont(context, item);
return convertView;
}
}
的onCreate
MySpinnerAdapter packageAdapter = (MySpinnerAdapter) ArrayAdapter
.createFromResource(this, R.array.packageList,
R.layout.packageitem);
R.layout.packageItem
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
style="@style/completedProminent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/tabSelected"
android:gravity="center"
android:layout_gravity="center" />
截至目前,我正在获取ClassCastException @onCreate(上面的源代码行)。我在这里做错了什么?
答案 0 :(得分:1)
试试这个..
MySpinnerAdapter packageAdapter = (MySpinnerAdapter)ArrayAdapter.createFromResource (this,R.layout.packageitem,R.array.packageList);.
构造函数将context, int, list
作为参数,并且您正在传递context, list, int.
这就是您获得ClassCastException的原因。
答案 1 :(得分:1)
最终起作用的是
private class MySpinnerAdapter extends ArrayAdapter<CharSequence> {
private Context context;
public MySpinnerAdapter(Context context, String[] objects, int textViewResourceId) {
super(context, textViewResourceId, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View returnObject = super.getView(position, convertView, parent);
TextView item = (TextView) findViewById(R.id.item);
FontUtils.setRobotoFont(context, item);
return returnObject;
}
}
<强>的onCreate 强>
MySpinnerAdapter packageAdapter = new MySpinnerAdapter(this,
getResources().getStringArray(R.array.packageList),
R.layout.packageitem);