I have a ListView with a custom adapter that extends CursorAdapter.
that ListView also has a footer view
when a list item is clicked (which is not the footer) in the OnListItemClickListener
I get a ClassCastException on
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)
the footer view is inflated and added to the listview before setting the adapter.
clicking on the footer works fine and
if I don’t add the footer to the listview clicking on a list item works fine
what is the problem and how do i fix it ?
EDIT:
my custom adapter extends CursorAdapter and overrides BindView and 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);
}
}
Well from the looks of the error it doesn’t seem to agree that your HeaderViewListAdapter can be casted to a CursorAdapter.
Why don’t you show us some of the code for your HeaderViewListAdapter?
Edit: It appears that becuase you have headers/footers in your ListView, the adapter you get back is a HeadListViewAdapter, which wraps your original adapter.
From Android Dev API:
http://developer.android.com/reference/android/widget/HeaderViewListAdapter.html
The following is all educated guesses, but try it out:
l.getAdapter <– HeaderListViewAdapter
((HeaderListViewAdapter)l.getAdapter).getWrappedAdapter() <– The custom adapter you created.
So instead of this:
Cursor c = ((CursorAdapter) l.getAdapter()).getCursor();
Try this:
I broke it up so that if it doesn’t work, you’ll know exactly which statement is erroring out.