我在android版本4.3中使用listview添加/删除页脚为listview交叉应用程序?

Ash*_*hok 13 android android-arrayadapter android-listview

我曾经ListView添加页脚视图,并删除它在Android版本4.4以上的工作正常但在Android版本4.3及以下的问题我使用以下代码添加页脚

listfortestmyfeed.addFooterView(footerView);
Run Code Online (Sandbox Code Playgroud)

并删除代码后的页脚

listfortestmyfeed.removeFooterView(footerView);
Run Code Online (Sandbox Code Playgroud)

在我的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)
Run Code Online (Sandbox Code Playgroud)

我没有发现错误请告诉我任何人都知道提前谢谢

mat*_*ash 42

这可能是由于在调用之前调用setAdapter()ListView 引起的.这在4.4之前的所有Android版本中都是必需的setFooterView()

实际上,直到我看到这个问题,我才知道这个限制已经放宽了KitKat ... :)

addFooterView()API级别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.
Run Code Online (Sandbox Code Playgroud)

同时,KitKat,这个限制放宽了:

/*
 * 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.
Run Code Online (Sandbox Code Playgroud)

如果你想与4.4之前兼容,你需要尊重呼叫顺序,即

  1. addFooterView(footer);
  2. setAdapter(adapter);
  3. removeFooterView(footer);