I am using the listview add/remove footer for listview cross app in android version 4.3?
Asked Answered
V

2

13

I used to the ListView add the footer view and also remove footer its worked fine in android version 4.4 above but problem in android version 4.3 and below I am using the following code for adding the footer

listfortestmyfeed.addFooterView(footerView);

and remove footer following code

listfortestmyfeed.removeFooterView(footerView);

remove footer showing class cast exception in my logcat

 07-11 20:07:49.665: E/ACRA(22818): com.sample.activities fatal error : com.sample.adapters.MyfeedAdapter cannot be cast to android.widget.HeaderViewListAdapter
    07-11 20:07:49.665: E/ACRA(22818): java.lang.ClassCastException: com.sample.adapters.MyfeedAdapter cannot be cast to android.widget.HeaderViewListAdapter
    07-11 20:07:49.665: E/ACRA(22818):  at android.widget.ListView.removeFooterView(ListView.java:390)
    07-11 20:07:49.665: E/ACRA(22818):  at com.sample.fragments.MyfeedNewFragment$FollowingBloopsdoinback.onPostExecute(MyfeedNewFragment.java:172)
    07-11 20:07:49.665: E/ACRA(22818):  at com.sample.fragments.MyfeedNewFragment$FollowingBloopsdoinback.onPostExecute(MyfeedNewFragment.java:1)
    07-11 20:07:49.665: E/ACRA(22818):  at android.os.AsyncTask.finish(AsyncTask.java:631)
    07-11 20:07:49.665: E/ACRA(22818):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
    07-11 20:07:49.665: E/ACRA(22818):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    07-11 20:07:49.665: E/ACRA(22818):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-11 20:07:49.665: E/ACRA(22818):  at android.os.Looper.loop(Looper.java:137)
    07-11 20:07:49.665: E/ACRA(22818):  at android.app.ActivityThread.main(ActivityThread.java:5103)
    07-11 20:07:49.665: E/ACRA(22818):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-11 20:07:49.665: E/ACRA(22818):  at java.lang.reflect.Method.invoke(Method.java:525)
    07-11 20:07:49.665: E/ACRA(22818):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    07-11 20:07:49.665: E/ACRA(22818):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    07-11 20:07:49.665: E/ACRA(22818):  at dalvik.system.NativeStart.main(Native Method)

i didn't findout the mistake please tell me anyone know advance thanks

Vermiculate answered 11/7, 2014 at 14:50 Comment(1)
I am used to add the footer in async class onpreexecute list.addFooterView(view)and onpostexecute() method list.removefooterView(view) but its worked in android version 4.4 above but showin g the error in android version 4.3 below showing above errorVermiculate
D
42

This is probably caused by calling setAdapter() on the ListView before calling setFooterView(). This was necessary in all versions of Android prior to 4.4

Actually, I didn't know this restriction had been relaxed for KitKat until I saw this question... :)

In the sources of addFooterView() for API level 15:

/*
 * NOTE: Call this before calling setAdapter. This is so ListView can wrap
 * the supplied cursor with one that will also account for header and footer
 * views.

Meanwhile, it KitKat, this restriction was relaxed:

/*
 * Note: When first introduced, this method could only be called before
 * setting the adapter with {@link #setAdapter(ListAdapter)}. Starting with
 * {@link android.os.Build.VERSION_CODES#KITKAT}, this method may be
 * called at any time.

If you want to be compatible with pre-4.4, you need to respect the calling order, i.e.

  1. addFooterView(footer);
  2. setAdapter(adapter);
  3. removeFooterView(footer);
Distributary answered 13/7, 2014 at 4:31 Comment(0)
W
2

Yes. Headers/footers are indeed problematically with backward compatibility. I recently refactored code, to use relative layout instead of a footer, which did pretty much, what I expected on any device -> footer visible, footer gone. Try this with a ListView footer, this will not work, instead you need to call addFooterView, removeFooterView and even this is not working as mentioned. The crash could be circumvented by

a = getAdapter()
setAdapter(null)
removeFooter()
setAdapter(a)

but the footer still was not working that way.

Wizardry answered 21/4, 2015 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.