I am trying to set a row to bold on clicking it. But it gives me a ClassCastException when I try to convert view into TextView like the following –
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);
}
});
What am I doing wrong? How should I go about it?
EDIT:
My adapter looks like –
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;
}
}
Your adapter code shows the main cause of failure:
According to your adapter code the
viewyou cast is the rowView, not the textView.So you should access it also that way. That’s why you getClassCastException.