问题背景: 最近写一个项目,根据屏幕左侧的字母查找好友,在网上找了个Demo,现在我想在显示好友的ListView上面增加一个HeaderView,运行的时候报了这个错误。
下面是Demo中的一个自定义View的代码:
public class SideBar extends View {
private char[] mL;
private SectionIndexer mSectionIndexter = null;
private ListView mList;
private TextView mDialogText;
private final int mM_nItemHeight = 15;
public SideBar(Context context) {
super(context);
init();
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mL = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z' };
}
public void setListView(ListView listView) {
mList = listView;
HeaderViewListAdapter ha = (HeaderViewListAdapter) listView.getAdapter();
mSectionIndexter = (SectionIndexer) ha.getWrappedAdapter();
// mSectionIndexter = (SectionIndexer) listView.getAdapter();
}
public void setTextView(TextView mDialogText) {
this.mDialogText = mDialogText;
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int i = (int) event.getY();
int idx = i / mM_nItemHeight;
if (idx >= mL.length) {
idx = mL.length - 1;
} else if (idx < 0) {
idx = 0;
}
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE) {
mDialogText.setVisibility(View.VISIBLE);
mDialogText.setText("" + mL[idx]);
if (mSectionIndexter == null) {
mSectionIndexter = (SectionIndexer) mList.getAdapter();
}
int position = mSectionIndexter.getPositionForSection(mL[idx]);
if (position == -1) {
return true;
}
mList.setSelection(position);
} else {
mDialogText.setVisibility(View.INVISIBLE);
}
return true;
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(0xff595c61);
paint.setTextSize(12);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / 2;
for (int i = 0; i < mL.length; i++) {
canvas.drawText(String.valueOf(mL[i]), widthCenter, mM_nItemHeight
+ (i * mM_nItemHeight), paint);
}
super.onDraw(canvas);
}
}
解决方法:
ListView增加HeaderView之后,将原来的Adapter包装成HeaderViewListAdapter,看看HeaderViewListAdapter的文档说明:
ListAdapter used when a ListView has header views. This ListAdapter wraps another one and also keeps track of the header views and their associated data objects.
This is intended as a base class; you will probably not need to use this class directly in your own code.
HeaderViewListAdapter有个方法getWrappedAdapter,该方法能返回被包装的HeaderViewListAdapter的ListAdapter。
到了这里就明白为什么会报ClassCastException异常了。因为ListView的getAdapter方法返回的是HeaderViewListAdapter的实例,而将其转换成BaseAdapter的子类的实例,肯定是不对的。
下面是我给显示好友的ListView设置HeaderView和Adapter的代码:
// 这里要注意,给ListView增加HeaderView和Adapter顺序,不然会报错。
LinearLayout headerView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.weibo_friends_listheader, null);
mFriendsContact.addHeaderView(headerView);
mFriendsContact.setAdapter(new FriendListViewAdapter(this, mNicks));
mIndexBar.setListView(mFriendsContact);
注意上面SideBar这个类中红色标记方法内的代码:
public void setListView(ListView listView) {
mList = listView;
//更改后的,我在获得ListView的Adapter之前进行了一下转换
HeaderViewListAdapter ha = (HeaderViewListAdapter) listView.getAdapter();
mSectionIndexter = (SectionIndexer) ha.getWrappedAdapter();
//更改前的
// mSectionIndexter = (SectionIndexer) listView.getAdapter();
}