I'm developing android music player and its my first app but there is something wrong in my code hence getting MainActivity(fragment tabs,ViewPager,naginational drawer code) can not cast PlayerActivity(music player ui,playing related stuff ) calling my player activity from songs Fragment.
My SongsFragment Code :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
songList = ((PlayerActivity) getActivity()).getSongs();
songView = (RecyclerView) rootView.findViewById(R.id.song_list);
songView.setHasFixedSize(true);
GridLayoutManager grid = new GridLayoutManager(getActivity(), 2);
grid.setOrientation(LinearLayoutManager.VERTICAL);
songView.setLayoutManager(grid);
getSongList();
// sort the tracks
Collections.sort(songList, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
((PlayerActivity) getActivity()).setSongs(songList);
SongAdapter songAdt = new SongAdapter(songList, getActivity());
songView.setAdapter(songAdt);
songView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
MusicService musicService = ((PlayerActivity) getActivity()).getMusicService();
musicService.setSong(position);
musicService.togglePlay();
}
}));
return rootView;
Player Activity Code has OnCreate Method ., also if anyone help me sorting songs list as album/artist . thanks