简体   繁体   English

将Arraylist从片段传递到其自己的Activity

[英]Pass Arraylist from fragment to its own Activity

I have a fragments that builds a songs ArrayList, i want to pass that ArrayList to the framgment's activity . 我有一个片段来构建歌曲ArrayList,我想将该ArrayList传递给片段的活动。 I know i can use interface, but not sure how i could do it 我知道我可以使用界面,但不确定如何去做

public class SongsListFragment extends Fragment
{

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


}

Here is what i have so far, please let me know why im getting NullPointer 这是我到目前为止的内容,请让我知道为什么我正在获取NullPointer

on MainActivity 在MainActivity上

    @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();

    }

on Fragment 在片段上

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);
            }
        });

on the method where i finish building the songArrayList, i did 在完成构建songArrayList的方法上,我做了

songsCallBack.onFragmentSetSongs(songArrayList);

Error When i Click on ListView 当我单击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)

Error Position 错误位置

 musicService.setSong(position);       //Main Activity (MainActivity.java:107)

 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            songsItemClick.onSongListItemClick(position);  < --  here
 }

Change name of your interface so that it begins from capital letter. 更改接口名称,使其以大写字母开头。 Then in your activity add this line to declaration of your activity: public class FragmentActivity extends Activity implements PassArrayList , override interface method. 然后在您的活动中将此行添加到活动的声明中: public class FragmentActivity extends Activity implements PassArrayList ,重写了接口方法。 And in fragment add following: 并在片段中添加以下内容:

PassArrayList mCallback;


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

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

And somewhere in your code, when you want to pass your list of songs back to the activity call the method onArticleSelected() on your mCallback object and pass to this method your arraylist. 在代码中的某个位置,当您要将歌曲列表传递回活动时,请调用mCallback对象上的mCallback onArticleSelected()方法,并将此方法传递给arraylist。 Then this arraylist will come as an argument to the method onArticleSelected() in your activity and you could make with it anything that you like. 然后,此数组列表将作为您活动中方法onArticleSelected()的参数,您可以使用它进行任何所需的操作。 But don't forget to nullify link to mCallback in onDetach() hook method to prevent context leak 但不要忘记在onDetach()挂钩方法中取消指向mCallback的链接,以防止上下文泄漏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM