简体   繁体   English

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

[英]get ListView in BroadcastReceiver error receiving broadcast intent

I'm writing a downloadable listview items and i used download manager. 我正在写一个可下载的listview项目,并且使用了下载管理器。 for updating listview after finishing every download i need to update row so i used BroadcastReceiver,my code is just fine for listfragment page but it just not working for listactivity page. 为了在每次下载完成后更新listview,我需要更新行,所以我使用了BroadcastReceiver,我的代码对于listfragment页面来说很好,但对于listactivity页面却不起作用。 Bellow is my code that has error: 贝娄是我的代码有错误:

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

the error is in this line f code: 错误在此行f代码中:

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

and this is logcat: 这是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

You've added a header view and/or a footer view to the ListView. 您已经向ListView添加了页眉视图和/或页脚视图。 Internally ListView will wrap the adapter it was given with another adapter, so that it can provide the header/footer views. 在内部,ListView将使用另一个适配器包装它给定的适配器,以便它可以提供页眉/页脚视图。 When y9ou call getAdapter() , it returns you this wrapper, which you cannot cast to your custom type (AllproAdapter). 当y9ou调用getAdapter() ,它将返回此包装器,您不能将该包装器转换为自定义类型(AllproAdapter)。

You can just keep a reference to your own adapter in your Activity/Fragment and use it directly. 您可以在“活动/片段”中保留对您自己的适配器的引用,然后直接使用它。

// 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM