gpt4 book ai didi

java - 将 Arraylist 从 fragment 传递到其自己的 Activity

转载 作者:行者123 更新时间:2023-12-01 12:16:30 24 4
gpt4 key购买 nike

我有一个构建歌曲 ArrayList 的 fragment ,我想将该 ArrayList 传递给该 fragment 的 Activity 。我知道我可以使用界面,但不确定如何做到这一点

public class SongsListFragment extends Fragment
{

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


}

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

在主 Activity 上

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

}

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

在我完成构建 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
}

最佳答案

更改界面名称,使其以大写字母开头。然后在您的 Activity 中添加以下行到您的 Activity 声明中:public class FragmentActivity extends Activity Implements PassArrayList,重写接口(interface)方法。并在 fragment 中添加以下内容:

PassArrayList mCallback;


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

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

在代码中的某个位置,当您想要将歌曲列表传递回 Activity 时,请调用 mCallback 对象上的方法 onArticleSelected() 并将其传递给此方法方法你的数组列表。然后这个数组列表将作为 Activity 中 onArticleSelected() 方法的参数,您可以用它制作任何您喜欢的东西。但不要忘记在 onDetach() 钩子(Hook)方法中取消指向 mCallback 的链接,以防止上下文泄漏

关于java - 将 Arraylist 从 fragment 传递到其自己的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26964741/

24 4 0