第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 recyclerview 適配器類中發(fā)送廣播?

如何在 recyclerview 適配器類中發(fā)送廣播?

HUX布斯 2022-07-14 09:38:23
我有一個RecyclerView顯示設(shè)備上找到的所有歌曲。適配器類holder.constraintLayout.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            //Store songList and songIndex in mSharedPreferences            storageUtil.storeSong(Main.musicList);            storageUtil.storeSongIndex(holder.getAdapterPosition());            //Send media with BroadcastReceiver            Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);            sendBroadcast(broadCastReceiverIntent);            Intent broadCastReceiverIntentUpdateSong = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_SONG);            sendBroadcast(broadCastReceiverIntentUpdateSong);        }    });我想要實現(xiàn)的是,當(dāng)在RecyclerView中單擊一首歌曲時,一個廣播被發(fā)送到我的服務(wù)類,因此一首歌曲開始播放。sendBroadcast 無法解決,那么如何從我的適配器類發(fā)送廣播意圖?我也想知道這是否是正確的方法,或者是否有更好的方法在適配器中發(fā)送廣播,因為我在某處讀到BroadcastReceivers不屬于適配器類。
查看完整描述

1 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

根本原因: sendBroadcast是Context類的方法,因為你在Adapter類中調(diào)用它,所以編譯器顯示錯誤“sendBroadcast 無法解析”。


解決方案:從視圖實例中獲取上下文,然后調(diào)用sendBroadcast方法。


holder.constraintLayout.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        //Store songList and songIndex in mSharedPreferences

        storageUtil.storeSong(Main.musicList);

        storageUtil.storeSongIndex(holder.getAdapterPosition());


        // Obtain context from view instance.

        Context context = v.getContext();


        //Send media with BroadcastReceiver

        Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);

        context.sendBroadcast(broadCastReceiverIntent);


        Intent broadCastReceiverIntentUpdateSong = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_SONG);

        context.sendBroadcast(broadCastReceiverIntentUpdateSong);

    }

});


查看完整回答
反對 回復(fù) 2022-07-14
  • 1 回答
  • 0 關(guān)注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號