在BroadcastReceiver中获取ListView错误接收广播意图

时间:2014-08-08 14:34:54

标签: android android-listview android-broadcast

我正在编写一个可下载的listview项目,我使用了下载管理器。为了在完成每次下载后更新listview我需要更新行,所以我使用了BroadcastReceiver,我的代码对于listfragment页面很好,但它只是不适用于listactivity页面。贝娄是我的错误代码:

BroadcastReceiver alldownloadReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       //check if the broadcast message is for our Enqueued download
       long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
       //final int position = intent.getIntExtra("position", -1);
       //Log.d("rrr", "dd");
       final AllproAdapter adapter = (AllproAdapter) getListView().getAdapter();

        //((AllproAdapter)((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
        //adapter.setDownloadFinished(referenceId);

        // Ask the adapter to refresh the ListView
        //adapter.notifyDataSetChanged();

      }
};//end of broadcast reciever

错误在此行代码中:

final AllproAdapter adapter = (AllproAdapter) getListView().getAdapter();

这是logcat:

08-08 14:21:01.997: E/AndroidRuntime(5286): FATAL EXCEPTION: main
08-08 14:21:01.997: E/AndroidRuntime(5286): java.lang.RuntimeException: Error     receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10000010 pkg=com.example.one (has extras) } in com.example.one.AllProductsActivity$1@40f2ae88
08-08 14:21:01.997: E/AndroidRuntime(5286):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at android.os.Handler.handleCallback(Handler.java:725)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at android.os.Looper.loop(Looper.java:137)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at java.lang.reflect.Method.invoke(Method.java:511)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at dalvik.system.NativeStart.main(Native Method)
08-08 14:21:01.997: E/AndroidRuntime(5286): Caused by: java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.example.one.AllproAdapter
08-08 14:21:01.997: E/AndroidRuntime(5286):     at com.example.one.AllProductsActivity$1.onReceive(AllProductsActivity.java:224)
08-08 14:21:01.997: E/AndroidRuntime(5286):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
08-08 14:21:01.997: E/AndroidRuntime(5286):     ... 9 more

1 个答案:

答案 0 :(得分:0)

您已向ListView添加了标题视图和/或页脚视图。在内部,ListView将使用另一个适配器包装它所提供的适配器,以便它可以提供页眉/页脚视图。当你打电话给getAdapter()时,它会返回你的这个包装器,你无法将其转换为自定义类型(AllproAdapter)。

您可以在Activity / Fragment中保留对自己适配器的引用,并直接使用它。

// class member
private AllproAdapter adapter;
...
// initialization
adapter = ...
setListAdapter(adapter);
...
// inside broadcast receiver
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
adapter.setDownloadFinished(referenceId);
adapter.notifyDataSetChanged();