问题背景: 最近写一个项目,根据屏幕左侧的字母查找好友,在网上找了个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();
}

可以参考这篇文章: http://hi.baidu.com/su350380433/item/3e70f16022b0471b7ddeccd8

更多相关文章

  1. android onSaveInstanceState方法
  2. Android(安卓)简单存储 SharedPreferencesUtils
  3. 浅析Android加载字体包及封装的方法
  4. Android与JS交互之基本
  5. 使用MediaPlayer播放声音的异常
  6. 【Android】listView动态处理item
  7. Android三种定时器的简单使用
  8. MainActivity has leaked window com.android.internal.policy.i
  9. 关于layout_weight的理解及使用方法

随机推荐

  1. the android development environment
  2. Android(安卓)startActivity rotate 3d a
  3. 4.6 RadioButton与RadioGroup的使用
  4. 检测Android是否连接WIFI
  5. 仿ios滚动 有弹性的ScrollView
  6. ContentObserver监听ContentProvider数据
  7. ToolBar基本使用及自定义ToolBar
  8. RecyclerView 简单分页加载
  9. android采用BroadcastReceiver实现定时器
  10. android handler和message的常用方法