我正在尝试在单击时将行设置为粗体。但是当我尝试将视图转换为 TextView 时,它给了我一个 ClassCastException,如下所示 -
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
tv.setTypeface(null, Typeface.BOLD);
}
});
我究竟做错了什么?我应该怎么做?
编辑:
我的适配器看起来像 -
public class CountryListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
public CountryListAdapter(Activity context, ArrayList<String> names) {
super(context, R.layout.rowlayout, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.rowlayout, null, true);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(names.get(position));
return rowView;
}
}