简体   繁体   中英

get ListView in BroadcastReceiver error receiving broadcast intent

I'm writing a downloadable listview items and i used download manager. 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. 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:

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

and this is 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. Internally ListView will wrap the adapter it was given with another adapter, so that it can provide the header/footer views. When y9ou call getAdapter() , it returns you this wrapper, which you cannot cast to your custom type (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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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