簡體   English   中英

用SearchView過濾后,Recyclerview不更新數據

[英]Recyclerview not updating data when filtered with searchview

所以我在帶有5個片段的選項卡式活動中有一個searchview,當我在片段1(Tab1)中搜索某些內容時,它會使用搜索到的歌曲正確過濾我的recyclerview。

但它仍會播放列表中的歌曲以及我所有的歌曲,而不是已過濾列表中的歌曲。

例如,在片段1中,我有5首歌曲,我搜索第5首歌曲,但是當我嘗試播放第5首歌曲時,它仍會播放我的第一首歌曲。

所以我的recyclerview不會用過濾后的arraylist更新我的arraylist。

Main.musicList是另一個活動的全局數組列表 ,該活動存儲設備中的所有歌曲。

StorageUtil是一個幫助程序類,它將我的arraylist與歌曲和歌曲索引一起存儲,以便我可以播放歌曲。

我怎樣才能做到這一點?

謝謝,

活動

@Override
public boolean onQueryTextSubmit(String query) {
    return true;
}

@Override
public boolean onQueryTextChange(String newText) {

    PagerAdapter pagerAdapter = mViewPager.getAdapter();
    for(int i = 0; i < pagerAdapter.getCount(); i++) {

        Fragment viewPagerFragment = (Fragment) mViewPager.getAdapter().instantiateItem(mViewPager, i);
        if(viewPagerFragment != null && viewPagerFragment.isAdded()) {

            if (viewPagerFragment instanceof Tab1){
                Tab1 tab1 = (Tab1) viewPagerFragment;
                if (tab1 != null){
                    tab1.beginSearch(newText); // Calling the method beginSearch of Tab1
                    if (newText == null || newText.trim().isEmpty()) {
                        tab1.resetSearch();
                    }
                }
            }else if (viewPagerFragment instanceof Tab2){
                Tab2 tab2 = (Tab2) viewPagerFragment;
                if (tab2 != null){
                    //tab2.beginSearch(query); // Calling the method beginSearch of Tab2
                }
            }else if (viewPagerFragment instanceof Tab3){
                Tab3 tab3 = (Tab3) viewPagerFragment;
                if (tab3 != null){
                    //tab3.beginSearch(query); // Calling the method beginSearch of Tab3
                }
            }else if (viewPagerFragment instanceof Tab4){
                Tab4 tab4 = (Tab4) viewPagerFragment;
                if (tab4 != null){
                    //tab4.beginSearch(query); // Calling the method beginSearch of Tab4
                }
            }else if (viewPagerFragment instanceof Tab5){
                Tab5 tab5 = (Tab5) viewPagerFragment;
                if (tab5 != null){
                    //tab5.beginSearch(query); // Calling the method beginSearch of Tab5
                }
            }
        }
    }        
    return false;

片段(Tab1)

private void initRecyclerView() {
    Main.musicList = Main.songs.songs;
    // Connects the song list to an adapter
    // (thing that creates several Layouts from the song list)
    if ((Main.musicList != null) && (!Main.musicList.isEmpty())) {
        allSongsAdapter = new AllSongsAdapter(getContext(), Main.musicList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerViewSongs.setLayoutManager(linearLayoutManager);
        recyclerViewSongs.setHasFixedSize(true);
        recyclerViewSongs.setAdapter(allSongsAdapter);
        Log.i(TAG, "Songs found in list!");

        recyclerViewSongs.addOnItemTouchListener(new OnItemClickListeners(getContext(), new OnItemClickListeners.OnItemClickListener() {
            @TargetApi(Build.VERSION_CODES.O)
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getContext(), "You Clicked position: " + position, Toast.LENGTH_SHORT).show();
                //Start playing the selected song.
                playAudio(position);

            }
        }));

    } else {
        Log.i(TAG, "No songs found in list!");
    }
}

public void beginSearch(String query) {
    Log.e("QueryFragment", query);
    allSongsAdapter.getFilter().filter(query);
}

public void resetSearch(){
    allSongsAdapter = new AllSongsAdapter(getContext(), Main.musicList);
    recyclerViewSongs.setAdapter(allSongsAdapter);
}

private void playAudio(int songIndex) {
    //MediaService is active
    //Store songList and songIndex in mSharedPreferences
    StorageUtil storageUtil = new StorageUtil(getContext());
    storageUtil.storeSong(Main.musicList);
    storageUtil.storeSongIndex(songIndex);
    //Send media with BroadcastReceiver
    Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
    if (getActivity() != null) {
        getActivity().sendBroadcast(broadCastReceiverIntent);
    }
    //Send media with BroadcastReceiver
    Intent broadCastReceiverIntentUpdateSong = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_SONG);
    if (getActivity() != null) {
        getActivity().sendBroadcast(broadCastReceiverIntentUpdateSong);
    }
}

帶有searchview過濾器的適配器

public Filter getFilter() {
    if (valueFilter == null) {
        valueFilter = new ValueFilter();
    }
    return valueFilter;
}

private class ValueFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String str = constraint.toString().toUpperCase();
        Log.e("constraint", str);
        FilterResults results = new FilterResults();

        if (constraint != null && constraint.length() > 0) {
            ArrayList<Song> filterList = new ArrayList<>();
            for (int i = 0; i < filteredMusicList.size(); i++) {
                if ((filteredMusicList.get(i).getTitle().toUpperCase())
                        .contains(constraint.toString().toUpperCase())) {
                    Song song = filteredMusicList.get(i);
                    filterList.add(song);
                }
            }
            results.count = filterList.size();
            results.values = filterList;
        } else {
            results.count = filteredMusicList.size();
            results.values = filteredMusicList;
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        musicList = (ArrayList<Song>) results.values;
        notifyDataSetChanged();
    }
}

編輯(正在工作,但不確定是否正確的方法)

在適配器中,我調用存儲音樂列表的助手類StorageUtil,並存儲過濾后的音樂列表,然后制作了broadcastreceiver,該廣播發送一個廣播,說明我的音樂列表已更新。

   @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        musicList = (ArrayList<Song>) results.values;
        notifyDataSetChanged();
        StorageUtil storageUtil = new StorageUtil(context);
        storageUtil.storeSong(musicList);

        Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_MUSICLIST);
        context.sendBroadcast(broadCastReceiverIntent);
    }

在我的片段中,我將音樂列表設置為過濾列表

  //Register BroadCastReceiver UPDATE_SONG
private void registerUpdateMusicListBroadCastReceiver(){
    IntentFilter intentFilter = new IntentFilter(Constants.ACTIONS.BROADCAST_UPDATE_MUSICLIST);
    if (getActivity() != null) {
        getActivity().registerReceiver(UpdateMusicListBroadCastReceiver, intentFilter);
    }

}

private BroadcastReceiver UpdateMusicListBroadCastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        StorageUtil storageUtil = new StorageUtil(getContext());
        Main.musicList = storageUtil.getSongs();
    }
};

問題是如何獲取音樂索引。 過濾音樂列表時,列表中只有一項。 因此,它在列表中的位置始終為0。 您應該在真實音樂列表(Main.musicList)中獲得所選項目的索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM