我有一个带有扩展 CursorAdapter 的自定义适配器的 ListView。ListView 也有一个页脚视图
当在 OnListItemClickListener 中单击列表项(不是页脚)时,我得到一个 ClassCastException
Cursor c = ((CursorAdapter)l.getAdapter()).getCursor();
E/AndroidRuntime( 8579): FATAL EXCEPTION: main
E/AndroidRuntime( 8579): java.lang.ClassCastException: android.widget.HeaderViewListAdapter
E/AndroidRuntime( 8579): at com.gbenhaim.dealsapp.BrowsePostsActivity.onListItemClick(BrowsePostsActivity.java:277)
E/AndroidRuntime( 8579): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
E/AndroidRuntime( 8579): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime( 8579): at android.widget.ListView.performItemClick(ListView.java:3513)
E/AndroidRuntime( 8579): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
E/AndroidRuntime( 8579): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 8579): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 8579): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 8579): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 8579): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 8579): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 8579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 8579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 8579): at dalvik.system.NativeStart.main(Native Method)
在设置适配器之前,页脚视图被膨胀并添加到列表视图中。
单击页脚工作正常,如果我不将页脚添加到列表视图单击列表项工作正常
有什么问题,我该如何解决?
编辑:我的自定义适配器扩展 CursorAdapter 并覆盖 BindView 和 NewView
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if (v == footer) {
...
} else {
Cursor c = ((CursorAdapter) l.getAdapter()).getCursor();
c.moveToPosition(position);
...
}
}
class PostAdapter extends CursorAdapter {
public PostAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// DISPLAY DATA in view
...
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.postrow, parent, false);
}
}