Sumama Waheed
Sumama Waheed

Reputation: 3609

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 . 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

on 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

songsCallBack.onFragmentSetSongs(songArrayList);

Error When i Click on 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
 }

Upvotes: 0

Views: 776

Answers (1)

Oleg Osipenko
Oleg Osipenko

Reputation: 2430

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. 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. 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. But don't forget to nullify link to mCallback in onDetach() hook method to prevent context leak

Upvotes: 1

Related Questions