Reputation: 9574
I have the following code, to set the font type on a 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" />
As of now I am getting a ClassCastException @onCreate (source line above). What am I doing wrong here ?
Upvotes: 1
Views: 92
Reputation: 9574
What finally worked was
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);
Upvotes: 1
Reputation: 4487
Try this..
MySpinnerAdapter packageAdapter = (MySpinnerAdapter)ArrayAdapter.createFromResource (this,R.layout.packageitem,R.array.packageList);.
Constructor is taking context, int, list
as parameter and you're passing context, list, int.
That is why you're getting ClassCastException.
Upvotes: 1