@Override
public void onFragmentSetSongs(ArrayList<Song> songs){
songsArrayList = songs;
}
@Override
public void onSongListItemClick(int position) {
musicService.setSong(position);
Song playSong = songsArrayList.get(position);
txtCurrentSongTitle.setText(playSong.getTitle());
txtCurrentSongTitle.requestFocus();
musicService.playSong();
btnPlayPause.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_pause));
if (playbackPaused) {
playbackPaused = false;
}
//setDownloaded();
}
片段上的
OnFragmentInteractionListener songsCallBack;
OnFragmentInteractionListener songsItemClick;
public interface OnFragmentInteractionListener {
public void onFragmentSetSongs(ArrayList<Song> s);
public void onSongListItemClick(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
songsCallBack = (OnFragmentInteractionListener) getActivity();
songsItemClick = (OnFragmentInteractionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onDetach() {
super.onDetach();
songsCallBack = null;
songsItemClick= null;
}
listView = (ListView) root.findViewById(R.id.listViewSongs);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
songsItemClick.onSongListItemClick(position);
}
});
在我完成构建songArrayList的方法上,我做了
songsCallBack.onFragmentSetSongs(songArrayList);
当我点击ListView 时出现错误
at com..activities.MainActivity.onSongListItemClick(MainActivity.java:107)
at com..fragments.SongsListFragment$1.onItemClick(SongsListFragment.java:194)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1478)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480)
错误位置
musicService.setSong(position); //Main Activity (MainActivity.java:107)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
songsItemClick.onSongListItemClick(position); < -- here
}