将Arraylist从片段传递给自己的Activity

时间:2014-11-17 02:46:47

标签: java android listview android-fragments arraylist

我有一个片段构建歌曲ArrayList,我想将该ArrayList传递给framgment的活动。我知道我可以使用界面,但不知道我怎么做呢

public class SongsListFragment extends Fragment
{

    public interface passArrayList {
            public void onArticleSelected(Uri articleUri);    
            // from android guide i don't know what to do
    }


}

这是我到目前为止,请让我知道为什么我得到NullPointer

上的

    @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
 }

1 个答案:

答案 0 :(得分:1)

更改界面名称,使其从大写字母开始。然后在您的活动中添加此行以声明您的活动:public class FragmentActivity extends Activity implements PassArrayList,覆盖接口方法。并在片段中添加以下内容:

PassArrayList mCallback;


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mCallback = (PassArrayList) getActivity();
}

@Override
public void onDetach() {
    super.onDetach();
    mCallback = null;
}

在您的代码中的某个位置,当您想要将歌曲列表传回活动时,请调用onArticleSelected()对象上的方法mCallback,然后将此方法传递给您的arraylist。然后,这个arraylist将作为你活动中方法onArticleSelected()的参数,你可以用它做任何你喜欢的事情。但是不要忘记在onDetach()钩子方法中取消与mCallback的链接以防止上下文泄漏